简体   繁体   中英

Facebook iOS SDK FBSDKAccessToken.currentAccessToken() is nil even when logged in?

I am getting some strange behavior with the FBSDKAccessToken:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    println(FBSDKAccessToken.currentAccessToken()) //prints nil

    if (FBSDKAccessToken.currentAccessToken() != nil)
    {
        // User is already logged in, do work such as go to next view controller.
        println("this never prints")
        self.generateAPILoginDetails()
    }
    else
    {
        let loginView : FBSDKLoginButton = FBSDKLoginButton()
        self.view.addSubview(loginView)
        loginView.center = self.view.center
        loginView.readPermissions = ["public_profile", "email", "user_friends"]
        loginView.delegate = self
        FBSDKProfile.enableUpdatesOnAccessTokenChange(true)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "onProfileUpdated:", name:FBSDKProfileDidChangeNotification, object: nil)
    }


}

I am trying to get the token, but it's always nil, even when the loginView button shows the "Log Out" message, indicating that the user is, indeed, logged into Facebook.

I figured it out:

You need to add:

 NSNotificationCenter.defaultCenter().addObserver(self, selector: "onTokenUpdated:", name:FBSDKAccessTokenDidChangeNotification, object: nil)

I had the wrong NSNotification name (FBSDKProfileDidChangeNotification).

Then you use your callback handler to get the token and do stuff with it!

See here for more info: http://www.andrewkouri.com/swift-1-2-and-facebooks-new-login-sdk/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM