Search code examples
iosiphonefacebookfacebook-sdk-3.0

Facebook login in IOS works on emulator but not on device with native app installed


Facing the following problem: need to perform Facebook login in IOS 7. The thing goes right on the emulator but when running it on device with native app installed, the session give not permission to perform the login.

Using the code to create session: enter image description here

The checarLogin method works like:

enter image description here

I have googled to find it and figured out that the device settings that control the permissions of Facebook app works differently in the two cases:

Emulator give the permission with no issues:

enter image description here

The following image was took from emulator too, but only to illustrate the way the device works by default:

enter image description here

So the question is: there is other way to handle the login when running in a real device with native Facebook app installed and make it accept the permissions without the need to change the iphone settings?


Solution

  • This is what I use in my apps, you have to allow the login UI.

    - (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI completionHandler:(FBSessionStateHandler)handler {
    
    // We pass this permissions array into our request.
    // I only request email, but there are many more options.
    //
    NSArray *permissions = @[@"email", @"basic_info"];
    
    return [FBSession
            openActiveSessionWithReadPermissions:permissions
            allowLoginUI:allowLoginUI
            completionHandler:^(FBSession *session,
                                FBSessionState state,
                                NSError *error) {
    
                if (handler)
                    handler(session, state, error);
            }];
    

    }

    And that's how you use it

            [self openSessionWithAllowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
    
                if (!error && status == FBSessionStateOpen) {
                    NSString *token = session.accessTokenData.accessToken;
    
                    // You're logged in!
    
                } else {
    
                    // Something wrong happened
    
                }
            }];