Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stay logged in with iOS Facebook SDK 2.4

I'm implementing Facebook login in my app using FBSDKLoginManager. Facebook docs seem to imply that this saves the token for subsequent app launches so that the user doesn't have to log in each time:

The FBSDKLoginManager sets this token for you and when it sets currentAccessToken it also automatically writes it to a keychain cache.

I've been trying to retrieve this token from said cache when the app launches, or display my app's Facebook login screen if it can't be found:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    [FBSDKProfile enableUpdatesOnAccessTokenChange:YES];

    if ([FBSDKAccessToken currentAccessToken]) {
        // user is logged in, continue to app's main screen
    } else {
        // show login screen
    }

    //...
}

This shows the login screen every single time. Facebook documentation doesn't explain how this very simple use case should be handled. Am I missing something?

like image 909
Emilien H Avatar asked Aug 03 '15 23:08

Emilien H


1 Answers

You need to call "[[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions]" BEFORE trying to get cached token.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

[[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];

...

like image 199
AlexeyVMP Avatar answered Oct 22 '22 13:10

AlexeyVMP