Search code examples
iosfacebookfacebook-ios-sdk

Facebook SDK 3.1.1 on iOS 6 logs in but token instantly expires


I'm implementing the new Facebook SDK and everything is seems in order except for after logging in. When the app takes me to Safari and I give it the 'Okay', it returns to the app and reports success:

- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state) {
        case FBSessionStateOpen: {
            NSLog(@"success");
        }

.
.
.

}

But when I check the status immediately after:

if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
    NSLog(@"Logged in");
} else {
    NSLog(@"Not logged in");
}

I'm given: Not logged in. I'm not getting any error messages. It seems like the token expires straight after it is issued. Would like some guidance on how to tackle this. This app was created from as a new project, but is an update for an existing app. However, the old app is not on my device when running the new one. Just thought I'd mention that should it be important.


Solution

  • that state is specific to this condition:

     /*! One of two initial session states indicating that a cached token was loaded;
         when a session is in this state, a call to open* will result in an open session,
         without UX or app-switching*/
        FBSessionStateCreatedTokenLoaded        = 1,
    

    Since the "success" NSLog is executed in the code you posted, your state is actually: FBSessionStateOpen. Which means you are logged in and have a valid access token (log out FBSession.activeSession.accessToken to validate). Anyway, I believe you should be good - your loggedIn/notLoggedIn condition was just invalid.