Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS Facebook SDK 4.01: Should I be using FBSDKAccessToken.currentAccessToken() to check if user is logged in?

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

like image 625
Nick Avatar asked Apr 26 '15 22:04

Nick


1 Answers

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/

like image 157
jeffjv Avatar answered Nov 20 '22 03:11

jeffjv