FBSession.activeSession.isOpen returns NO even though the user logged-in

拟墨画扇 提交于 2019-11-28 00:10:51

It's just a facebook behavior, you need to 'reopen' the session everytime you launch the app, your guess is sort of right, It's because of the token state, a simple solution is that you check the [FBSession activeSession].isOpen state and if it returns NO call the openActiveSessionWithAllowLoginUI << Yes the same facebook method, but checking all the states it returns.

If the session was previously opened then it uses the stored token to reopen the session, you shouldn't worry about the login UI because it won't show up again as the session was previously opened.

if (![FBSession activeSession].isOpen) {
   [self connectWithFacebook];
}

- (void) connectWithFacebook { 
       // The user has initiated a login, so call the openSession method
       // and show the login UI if necessary << Only if user has never 
       // logged in or ir requesting new permissions.
       PPAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
       [appDelegate openSessionWithAllowLoginUI:YES];
}

And in the app delegate

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI
{
    NSArray *permissions = @[@"any_READ_permision_you_may_need"];

    return [FBSession openActiveSessionWithReadPermissions:permissions
                                              allowLoginUI:allowLoginUI
                                         completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
                                              if (error) {
                                                  NSLog (@"Handle error %@", error.localizedDescription);
                                              } else {
                                                  [self checkSessionState:state];
                                              }
                                         }];
}

- (void) checkSessionState:(FBState)state {
    switch (state) {
        case FBSessionStateOpen:
            break;
        case FBSessionStateCreated:
            break;
        case FBSessionStateCreatedOpening:
            break;
        case FBSessionStateCreatedTokenLoaded:
            break;
        case FBSessionStateOpenTokenExtended:
            // I think this is the state that is calling
            break;
        case FBSessionStateClosed:
            break;
        case FBSessionStateClosedLoginFailed:
            break;
        default:
            break;
    }
}

It's a quick fix and it works, you can reproduce it also just by closing the session but without clearing the token cache using [[FBSession activeSession] close] You can even change the token cache policy as described here http://developers.facebook.com/docs/howtos/token-caching-ios-sdk/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!