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

后端 未结 1 504
-上瘾入骨i
-上瘾入骨i 2020-12-06 12:38

I implemented the whole iOS Facebook login process in one of my iOS Game. In the app, you can either log-in with an email account or through Facebook. I occasionally presen

相关标签:
1条回答
  • 2020-12-06 13:19

    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/

    0 讨论(0)
提交回复
热议问题