xcode 快速 Facebook 登录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24371672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:07:34  来源:igfitidea点击:

Swift Facebook Login

iosxcodefacebookloginswift

提问by Tobias

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!

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

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];
                            }];}

Thanks, Tobias

谢谢,托比亚斯

回答by Chitra Khatri

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

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

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")
    }
}

On Button click do Facebook login

在按钮上点击做 Facebook 登录

 @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)
        })
    }

}

回答by NS Spartan029

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
        if FBSession.activeSession.state.value == FBSessionStateCreatedTokenLoaded.value {
            FBSession.openActiveSessionWithReadPermissions(self.facebookReadPermissions, allowLoginUI: true, completionHandler: {(session, state, error) -> Void in
                    self.sessionStateChanged(session, state: state, error: error)
                })
        }

}    
func sessionStateChanged(session:FBSession, state:FBSessionState, error:NSError?) {

}

回答by Rohit

This is the code

这是代码

if FBSession.activeSession().state == FBSessionStateCreatedTokenLoaded
    {
        FBSession.openActiveSessionWithPublishPermissions("publish_actions", defaultAudience: FBSessionDefaultAudienceFriends, allowLoginUI: true, completionHandler: ^(session : FBSession, state : FBSessionState, error : NSError))
            {
                // 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)
        }
    }
           return true
}

func sessionStateChanged(session : FBSession, state : FBSessionState, error : NSError)
{
    // If the session was opened successfully
    if  state == FBSessionStateOpen
    {
        println("Session Opened")
    }
}

回答by Scott Gardner

This can be simplified to:

这可以简化为:

let state = FBSession.activeSession().state

if state == .Open && state == .OpenTokenExtended {
  FBSession.activeSession().closeAndClearTokenInformation()
} else {
  FBSession.openActiveSessionWithReadPermissions(["public_profile"], allowLoginUI: true) {
    _ in
    FBSession.activeSession().closeAndClearTokenInformation()
  }
}