Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook authorization fails on iOS6 when switching FB account on device

Tags:

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 falling back to the normal authorization via web view (I do not have the native Facebook application installed in both cases). The problem occurs when I switch the account on iOS level. Logging out and logging in with a different FB user account.

To log in/authorize I perform:

[FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:allowLoginUI                                      completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {                                          [self sessionStateChanged:session state:state error:error];                                      }]; 

If then get a FBSessionStateClosedLoginFailed every time even though I perform a closeAndClearTokenInformation when that state is reached:

- (void)sessionStateChanged:(FBSession *)session                   state:(FBSessionState) state                   error:(NSError *)error {     NSLog(@"Session State Changed: %u", [[FBSession activeSession] state]);     switch (state) {         case FBSessionStateOpen:             break;         case FBSessionStateClosed:         case FBSessionStateClosedLoginFailed:             NSLog(@"FBSessionStateClosedLoginFailed ERROR: %@", [error description]);             [[FBSession activeSession] closeAndClearTokenInformation];             break;         default:             break; } 

However, I receive the same state on every retry. My log says the following:

FBSDKLog: FBSession **INVALID** transition from FBSessionStateCreated to FBSessionStateClosed FBSDKLog: FBSession transition from FBSessionStateCreated to FBSessionStateCreatedOpening  FBSDKLog: FBSession transition from FBSessionStateCreatedOpening to FBSessionStateClosedLoginFailed Session State Changed: 257 FBSessionStateClosedLoginFailed TOKEN: (null) FBSessionStateClosedLoginFailed ERROR: Error Domain=com.facebook.sdk Code=2 "The operation couldn’t be completed. (com.facebook.sdk error 2.)" UserInfo=0xb24cc20 {com.facebook.sdk:ErrorLoginFailedReason=com.facebook.sdk:ErrorLoginFailedReason} 

Can anyone reproduce this or has any idea where the problem might lie?

like image 936
Kathrin Holweger Avatar asked Oct 11 '12 11:10

Kathrin Holweger


People also ask

Why can't I login my facebook account on Iphone?

If you're having trouble logging into your Facebook account from your Facebook app: Make sure that you have the latest version of the Facebook app, or delete the app and then reinstall it. Try logging in from a mobile browser (example: Safari, Chrome).

How do I reauthorize my Facebook account?

To reauthorize your Facebook Page, go to Admin > Channels > Facebook and click on the Reauthorize button. It will take you to the Facebook login page, where you will have to enter the Admin credentials of the page that you are trying to reauthorize.

Why do I have to login to Facebook?

Re-authentication enables your app to confirm a person's identity even if it was verified previously. Facebook Login lets your app ask a person to re-enter their Facebook password at any time.


2 Answers

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 <Accounts/Accounts.h> 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!

like image 50
ill_always_be_a_warriors Avatar answered Sep 19 '22 12:09

ill_always_be_a_warriors


I had the same problem. Check that your FB App is enabled in Settings -> Facebook. Mine was disabled (even though I don't remember disabling it) and once I enabled it, it was fixed.

In my test process, I've added and removed my FB App several times from my FB Account, which is linked with my iPhone. It may explain why, magically, my app was disabled.

like image 28
Pierre-Olivier Simonard Avatar answered Sep 20 '22 12:09

Pierre-Olivier Simonard