Facebook authorization fails on iOS6 when switching FB account on device

后端 未结 6 2173
难免孤独
难免孤独 2020-11-28 04:14

I\'m using die Facebook SDK 3.1.1 to implement FB Connect in my iOS application. This works fine in the simple case with either the new FB integration (logged in on iOS) or

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-28 04:42

    Another answer gives a way to manually resync the device with the server. I defined a method called fbRsync to call this code. Make sure to #import in your implementation file and then define this method:

    -(void)fbResync
    {
        ACAccountStore *accountStore;
        ACAccountType *accountTypeFB;
        if ((accountStore = [[ACAccountStore alloc] init]) && (accountTypeFB = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook] ) ){
    
        NSArray *fbAccounts = [accountStore accountsWithAccountType:accountTypeFB];
        id account;
        if (fbAccounts && [fbAccounts count] > 0 && (account = [fbAccounts objectAtIndex:0])){
    
        [accountStore renewCredentialsForAccount:account completion:^(ACAccountCredentialRenewResult renewResult, NSError *error) {
            //we don't actually need to inspect renewResult or error.
            if (error){
    
            }
        }];
    }
    

    }

    I then call fbResync if openActiveSessionWithReadPermissions yields an error:

    [FBSession openActiveSessionWithReadPermissions:permissions
                                       allowLoginUI:YES
                                  completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
         if(error)
         {
             NSLog(@"Session error");
             [self fbResync];
             [NSThread sleepForTimeInterval:0.5];   //half a second
             [FBSession openActiveSessionWithReadPermissions:permissions
                                                allowLoginUI:YES
                                           completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
                [self sessionStateChanged:session state:state error:error];
                                           }];
    
         }
         else
             [self sessionStateChanged:session state:state error:error];
     }];
    

    The half a second delay is likely unnecessary, but it currently gives me piece of mind.

    This seems to solve the problem for me. I can now switch between Facebook accounts and am able to log in. Yay!

提交回复
热议问题