ios6 facebook integration login always FBSessionStateClosedLoginFailed never opens

回眸只為那壹抹淺笑 提交于 2019-11-30 11:18:12

I had the same problem, and it was because I forgot to implement

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

in my AppDelegate

While JohnG's answer is correct, that particular delegate method has now been deprecated. The correct delegate method to use is this:

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

Placing that in my application delegate solved the issue for me. Without this, the iOS 6.0 native login would work but the Facebook app login would fail.

I had the same problem. If the user hadn't a Facebook account configured in the System Preferences the Facebook login dialog appeared and everything worked fine. But if the user had an account configured in the device I was always having a FBSessionStateClosedLoginFailed error.

It has nothing to do with handleOpenURL:url because in the "native login flow" for iOS 6 or greater there is no fast-app-change. So your app never goes to background and the URL redirection is not needed.

The problem was solved configuring the Facebook app. You need to enable the "Native iOS login" section and put the bundle ID of your app. If your app is not already in the Apple Store I'm afraid you need to set the sandbox mode to ON. Once your app is published in the Apple Store I think you need to put the sandbox mode to OFF and set the Apple Store ID in the Facebook configuration.

Even i can't comment so writing it as answer . I am not sure , let me analyze potential reasons 1) you said: "but it is never called! shouldn't it get called?" -- for

- (BOOL)application:(UIApplication *)application 
        openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication
     annotation:(id)annotation

to get called , in the info.plist file , url scheme should be set properly i.e go to URL types-->Item0-->URL Schemes--->Item 0 the value here should be fbAPPID i.e fb append with your actual facebookappID [FacebookAppID could also be present in the info.plist or hard coded in code].

2)You said: "sessionStateChanged Callback always ends up in FBSessionStateClosedLoginFailed"

In the sessionStateChanged switch method check the state of session and if it created or createdTokenLoaded then try to open the session.

Note:[FBSession openActiveSessionWithPublishPermissions ....] which is a static method will create a new session and is set to FBSession.activeSession where as openWithCompletionHandler is an instance method which can work only if the session is already created .

    case FBSessionStateCreated:
    case FBSessionStateCreatedTokenLoaded:{
        [session openWithCompletionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
            //handle the updating of views according to the sate of session 
        }];
        break;

I agree with JohnG, with the additional caveat of:

Note that if you have multiple cases inside of handleOpenURL:, you can filter for:

if ([sourceApplication isEqualToString:@"com.facebook.facebook"] ||
    [sourceApplication isEqualToString:@"com.facebook.Facebook"]){
  //Do Stuff
}

It's weird, but I was able to replicate on multiple devices the sourceApplication coming back as "Facebook" and "facebook".

On FB SDK 3.5, You can kill the token with this:

    [[FBSessionTokenCachingStrategy defaultInstance] clearToken];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!