简体   繁体   中英

Initial FBSession check consistently fails

I have an app in which the user may choose to login to FB. My code is based largely on the tutorials at FB, and for the most part, the app, and the FB integration works as expected. The problem I am having is that the app is not remembering from launch to launch that the user has selected to connect the app to FB. I put a check into AppDelegate.m to check for a cached FBSession:

   if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
        // Yes, so just open the session (this won't display any UX).
        NSLog(@"The state is IS 'State created token loaded'");
        [self openSessionWithAllowLoginUI:NO];
    } else {
        // No, display the login page.
        NSLog(@"The state is NOT 'State created token loaded'");
        [self openSessionWithAllowLoginUI:YES];
    }

Every time I launch the app, the line "The state is NOT 'State created token loaded'" is displayed in the console. This makes me think I am not doing something right in order to make that FB login persist from launch to launch.

I could really use some advice here. What does one need to do to ensure that "FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded" is true on app launch?

If you have this code right when the app starts, then note that:

FBSession.activeSession

May not be set yet. What you want to do to check for a cached token is something like:

if (![self openSessionWithAllowLoginUI:NO]) {
    [self openSessionWithAllowLoginUI:YES];
}

The first call with the "NO" will return synchronously with a value of true if there was aa cached token. It returns no if there is no cached token. At this point you can force the login UX to happen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM