Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 407
Andrew K Avatar asked May 20 '15 05:05

Andrew K


1 Answers

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/

like image 139
Andrew K Avatar answered Sep 28 '22 05:09

Andrew K