9

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

Nick
  • 19,198
  • 51
  • 185
  • 312

1 Answers1

22

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/

jeffjv
  • 3,461
  • 2
  • 21
  • 28
  • 2
    Hmm, this seems to work upon the "initial" facebook login (when the user has to go out to facebook and authorize your app), but then when you subsequently launch the app (and the button says "Log Out") the token is still nil. – Andrew K May 18 '15 at 15:10
  • @AndrewK That's not what I've experienced. If you are seeing the button say "Log out" then currentAccessToken should not be nil, otherwise it would say "Log in with facebook". It is using the access token behind the scenes to display the correct button text. Maybe you could try posting your code in a new question. – jeffjv May 19 '15 at 01:46
  • Thanks, it works after returning this from didFinishLaunchingWithOptions return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions) – kalpeshdeo May 29 '15 at 10:01
  • 1
    Since FBSDKAccessToken.currentAccessToken() does not return an optional, won't it always be non-nil? – toddg Mar 31 '16 at 17:09
  • 3
    I have the AppDelegate method called on time. I still get the nil accessToken. from [FBSDKAccessToken currentAccessToken] – omarojo Sep 09 '16 at 03:53
  • @jeffjv Can I get currentAccessToken if the user has updated new version of my app assuming that user was logged in the previous version of App. – Manish Nahar Jan 11 '18 at 13:25