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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With