iOS Facebook SDK Error Domain com.facebook.sdk Code 2 and Code 7

Artem Z. picture Artem Z. · Dec 18, 2013 · Viewed 19.7k times · Source

I am developing the application which allow user to login via Facebook (using Facebook SDK for it). The error appears when a user has already logged in Facebook in iPhone settings. If not - all work correctly.

NSArray *permissions = [[NSArray alloc] initWithObjects:@"email", nil];
    [FBSession openActiveSessionWithReadPermissions:permissions
                                       allowLoginUI:YES
                                  completionHandler:
     ^(FBSession *session,
       FBSessionState state, NSError *error) {
         [self fbSessionStateChanged:session state:state error:error];
     }];

I've already tried to set permissions as nil array - nothing changed.

The log is:

Error Domain=com.facebook.sdk Code=2 "The operation couldn’t be completed.
(com.facebook.sdk error 2.)" UserInfo=0x1552c6c0 
{com.facebook.sdk:ErrorLoginFailedReason=com.facebook.sdk:SystemLoginDisallowedWithoutError,
com.facebook.sdk:ErrorSessionKey=<FBSession: 0xabe8100, state: FBSessionStateClosedLoginFailed,
loginHandler: 0x0, appID: APPIDHERE, urlSchemeSuffix: ,
tokenCachingStrategy:<FBSessionTokenCachingStrategy: 0x14b8f3d0>,
expirationDate: (null), refreshDate: (null),
attemptedRefreshDate: 0001-12-30 00:00:00 +0000, permissions:(null)>}

Sometimes the error with Code 7 is appears too. I have read almost all topics related to this error.

My steps were:

  1. Compare my app id in .plist file with FB bundle id. They are the same!!!
  2. My app is not in a sandbox mode!
  3. If I change from [FBSession openActiveSessionWithReadPermissions:permissions to [FBSession openActiveSessionWithPermissions:permissions- it works. But it is deprecated.

Answer

Mahesh picture Mahesh · Dec 18, 2013

Yes, after you see this error, if you go to Settings, you will see that the setting for this app is turned "OFF". But the problem in this case is that the user was never prompted to allow access -- i.e. the setting was turned to OFF automatically on first time access. If the user was asked, then of course that is understandable, but this is not the case (it's as if the SDK silently and automatically pressed Don't Allow for the user). That's why this is a problem.

Before you read any further, I want to note that once the setting is set, you cannot simply repeat the process to test it, because once the setting is set, it will never ask the user (even deleting and reinstalling the app does not help). To test this issue, you need to reset the permissions by going to Settings -> General -> Reset -> Reset Location & Privacy, before you can try to replicate this again.

From testing, I've discovered that if you have offline_access in the permissions you are requesting for the first time, then it will give this login error (and not prompt the user and set the permission to OFF). The SDK does not check and tell you that this permission is not allowed; it just fails to login.