Facebook SDK 3.1.1 & iOS 6.1 Login error

2019-07-17 03:10发布

问题:

I know this is going to seem like a duplicate question, but I've looked at every single question on stack overflow relating to this and I'm still getting the following error:

Error Domain=com.facebook.sdk Code=2 "The operation couldn’t be completed. 
(com.facebook.sdk error 2.)" UserInfo=0x1ecbf8e0       
{com.facebook.sdk:ErrorLoginFailedReason=com.facebook.sdk:ErrorLoginFailedReason}

That's the entire NSLog of the NSError..

I have followed the tutorials for authentication to the letter;

  • On developers.facebook.com I have made my application a Native iOS app, and put in my bundle id
  • In my info.plist I have included the URL types > Item 0 > URL Schemes > Item 0 > fb123456 and FacebookAppID = 123456, keys.

I am attempting to authenticate using the following code;

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

Permissions is set to nil.

I also have the following set in my app delegate

- (void)applicationDidBecomeActive:(UIApplication *)application
{
[FBSession.activeSession handleDidBecomeActive];
}

- (void)applicationWillTerminate:(UIApplication *)application
{
[FBSession.activeSession close];
}

- (BOOL)application:(UIApplication *)application
        openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
     annotation:(id)annotation {
return [FBSession.activeSession handleOpenURL:url];
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
return [FBSession.activeSession handleOpenURL:url];
}

One thing I have noted is that the error message hits my screen near instantly after I release the button, I put the phone into airplane mode and the speed was the same, I am thinking that the command is not even leaving the phone and going to the facebook server to begin with.. I definitely have network connection as I access a google api moments before the facebook call.

Edit:

If I use the deprecated method below

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

I am able to authenticate with facebook.

The session returned is

<FBSession: 0x20038450, state: FBSessionStateOpen, loginHandler: 0x2006ba90, appID: 3895930xxxxxxxx, urlSchemeSuffix: , tokenCachingStrategy:<FBSessionTokenCachingStrategy: 0x1ed4ce30>, expirationDate: 2013-04-08 16:25:19 +0000, refreshDate: 2013-02-07 16:25:19 +0000, attemptedRefreshDate: 0001-12-30 00:00:00 +0000, permissions:(
)>

compared with this session from the failed attempt with the updated 3.1.1 methods

<FBSession: 0x1e8970b0, state: FBSessionStateClosedLoginFailed, loginHandler: 0x0, appID: 3895930xxxxxxxx, urlSchemeSuffix: , tokenCachingStrategy:<FBSessionTokenCachingStrategy: 0x1d5853b0>, expirationDate: (null), refreshDate: (null), attemptedRefreshDate: 0001-12-30 00:00:00 +0000, permissions:(
)>

I'm assuming the missing loginHandler object gets created upon successful login.

While this allows me to continue developing the rest of my app with my logged in token, this is not acceptable for me to send my app live. Does anyone know why this is happening/what might be causing this?

回答1:

I have got the same error on 2 instances.

  1. When the switch is not turned on in your iPhone under Settings -> Facebook -> YourAppName in order to allow your app to use the facebook account. In this case I am displaying an alert asking the user to turn the switch on for the app.

  2. When the bundle identifier of your app does not match the bundle identifier that you specified on Facebook App Settings page for your app.

Since the error occurs for you even without internet connection I think it should be case 1.



回答2:

I had a similar issue. the error comes up when the i switched the fb user from setting. Facebook authorization fails on iOS6 when switching FB account on device This solved my problem