Swift Facebook登录 [英] Swift Facebook Login

查看:358
本文介绍了Swift Facebook登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将以下代码行从Objective C转换为新的编程语言Swift。也许有人可以帮助我并概述差异。太棒了!

I'm trying to convert the following code lines from Objective C to the new programming language Swift. Maybe someone can help me and outline the differences. Would be awesome!

if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
 [FBSession openActiveSessionWithReadPermissions:@[@"public_profile"]
                                 allowLoginUI:NO
                            completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
                              // Handler for session state changes
                              // This method will be called EACH time the session state changes,
                              // also for intermediate states and NOT just when the session open
                              [self sessionStateChanged:session state:state error:error];
                            }];}

谢谢,托比亚斯

推荐答案

这是我的答案:很少有关键字如 FBSessionStateCreatedTokenLoaded 为我抛出错误..所以这可能会有所帮助

Here is my answer: Few keywords like FBSessionStateCreatedTokenLoaded thrown error for me.. So This might helps

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

  // Whenever a person opens the app, check for a cached session
    if FBSession.activeSession().state == FBSessionState.CreatedTokenLoaded
    {

         // If there's one, just open the session silently, without showing the user the login UI
        FBSession.openActiveSessionWithReadPermissions(["public_profile"], allowLoginUI: false, completionHandler: {
            (session, state, error) -> Void in
             self.sessionStateChanged(session, state: state, error: error)
        })
    }

 return true
}
 func sessionStateChanged(session : FBSession, state : FBSessionState, error : NSError?)
{
    // If the session was opened successfully
    if  state == FBSessionState.Open
    {
        println("Session Opened")
    }
    // If the session closed
    if state == FBSessionState.Closed
    {
        println("Closed")
    }
}

点击按钮点击Facebook登录

On Button click do Facebook login

 @IBAction func FacebookLoginPressed(Sender: AnyObject)
{
   if (FBSession.activeSession().state == FBSessionState.Open || FBSession.activeSession().state == FBSessionState.OpenTokenExtended)
    {
        // Close the session and remove the access token from the cache
        // The session state handler (in the app delegate) will be called automatically
        FBSession.activeSession().closeAndClearTokenInformation()
    }
    else
    {
        // Open a session showing the user the login UI
        // You must ALWAYS ask for public_profile permissions when opening a session
        FBSession.openActiveSessionWithReadPermissions(["public_profile"], allowLoginUI: true, completionHandler: {
            (session:FBSession!, state:FBSessionState, error:NSError!) in

            let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
             // Call the app delegate's sessionStateChanged:state:error method to handle session state changes
            appDelegate.sessionStateChanged(session, state: state, error: error)
        })
    }

}

这篇关于Swift Facebook登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆