I getting trying to get familiar with FB's newest IOS SDK (4.0.1). I've integrated it into an IOS8 Swift project and using the FBSDKLoginButton to log users in and out. I want the application to skip showing the Login view controller if the user has already logged in.
Should I just be checking the return result of FBSDKAccessToken currentAccessToken()? Does this return 'nil' if a user is not logged in? The docs have the following to say about this method:
You can load this with the SDK from a keychain cache or from an app bookmark when your app cold launches. You should check its availability in your view controller's viewDidLoad.
Something like:
// LoginViewController.swift
override func viewDidLoad() {
super.viewDidLoad()
if(FBSDKAccessToken.currentAccessToken()){
//They are logged in so show another view
}else{
//They need to log in
}
}
So this sounds like it might be what I'm looking for but I can't really tell from this description. How are the good people of SO handling this common use case? :)
-Nick
Yes, FBSDKAccessToken.currentAccessToken() is what you should be checking although the code you have won't work as this does not return a Boolean. You'll need something like:
if(FBSDKAccessToken.currentAccessToken() != nil) {
//They are logged in so show another view
} else {
//They need to log in
}
Also, this alone only works for when the app moves between foreground/background (user hits home button). If the app is killed and cold launched the token will be lost unless you implement the following in didFinishlaunchingWithOptions in the AppDelegate:
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
You do not need to worry about the keychain as this is handled for you automatically provided you implement the necessary parts.
The following website has a good tutorial of getting a simple example working: http://www.brianjcoleman.com/tutorial-how-to-use-login-in-facebook-sdk-4-0-for-swift/
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