4

I have just found this issue whilst testing my application and it is really starting to annoy me. So what the environment is like is as follows :

  • No Facebook App installed
  • User is logged into iOS System Account (Under settings -> Facebook)

When my app attempts to authenticate the user for the first time it provides this wall of text : ( I tried cleaning it up a little)

2014-01-30 15:20:31.439 Unifeed[2140:70b] Session is <FBSession: 0xb74f9e0, state:         
FBSessionStateClosedLoginFailed, 
loginHandler: 0x0, appID: *************, urlSchemeSuffix: ,    
 tokenCachingStrategy:<FBSessionTokenCachingStrategy: 0xb73bd90>,
 expirationDate: (null),     
refreshDate: (null), attemptedRefreshDate: 0001-12-30 00:00:00 +0000, permissions:(null)>

2014-01-30 15:20:31.440 Unifeed[2140:70b] Session closed
2014-01-30 15:20:31.440 Unifeed[2140:70b] User logged out

2014-01-30 15:20:31.441 Unifeed[2140:70b] Error occured Error Domain=com.facebook.sdk Code=2    
"The operation couldn’t be completed. (com.facebook.sdk error 2.)"
UserInfo=********  
{com.facebook.sdk:ErrorLoginFailedReason=com.facebook.sdk:SystemLoginCancelled,    
com.facebook.sdk:ErrorInnerErrorKey=Error Domain=com.apple.accounts Code=7 "The operation 
couldn’t be completed. (com.apple.accounts error 7.)", 
com.facebook.sdk:ErrorSessionKey=<FBSession: 0xb74f9e0, state: FBSessionStateClosedLoginFailed,   
loginHandler: 0x0, appID: ************, urlSchemeSuffix: 
, tokenCachingStrategy:<FBSessionTokenCachingStrategy: 
0xb73bd90>, expirationDate: (null), refreshDate: (null), 
attemptedRefreshDate: 0001-12-30 00:00:00 +0000, permissions:(null)>}

2014-01-30 15:20:31.441 Unifeed[2140:70b] User cancelled login
2014-01-30 15:20:31.445 Unifeed[2140:70b] User logged out

I know thats slightly confusing but my take on it is its erring out because it thinks the user cancelled the login process. The thing is I never see the little pop up asking me if I want to login or to accept the permissions.... So I added the Facebook app to the phone and it worked... well thats great but what if I have a user with not App but they have FB logged in on the phone.... Anyone have any ideas?

My code for authentication is as follows : (Pretty much just a little modified from FB website)

- (void) facebookSessionChange {
// If the session state is any of the two "open" states when the button is clicked
if (FBSession.activeSession.state == FBSessionStateOpen
    || FBSession.activeSession.state == FBSessionStateOpenTokenExtended) {

    // Close the session and remove the access token from the cache
    // The session state handler (in the app delegate) will be called automatically
    [FBSession.activeSession closeAndClearTokenInformation];

    // If the session state is not any of the two "open" states when the button is clicked
} else {
    // Open a session showing the user the login UI
    [FBSession openActiveSessionWithReadPermissions:permissions
                                       allowLoginUI:YES
                                  completionHandler:
     ^(FBSession *session, FBSessionState state, NSError *error) {
         [self sessionStateChanged:session state:state error:error];
     }];
}
 }

 - (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error
 {

NSLog(@"Session is %@", session);
// If the session was opened successfully
if (!error && state == FBSessionStateOpen){
    NSLog(@"Session opened");
    // Show the user the logged-in UI
    [self userLoggedIn];
    return;
}
if (state == FBSessionStateClosed || state == FBSessionStateClosedLoginFailed){
    // If the session is closed
    NSLog(@"Session closed");
    // Show the user the logged-out UI
    [self userLoggedOut];
}

// Handle errors
if (error){
    NSLog(@"Error occured %@", error);
    if ([FBErrorUtility shouldNotifyUserForError:error] == YES){
        NSLog(@"Error occured %@", error);
    } else {

        // If the user cancelled login, do nothing
        if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
            NSLog(@"User cancelled login");

            // Handle session closures that happen outside of the app
        } else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession){
            NSLog(@"Session is no longer valid");

        } else {
            //Get more error information from the error
            NSDictionary *errorInformation = [[[error.userInfo objectForKey:@"com.facebook.sdk:ParsedJSONResponseKey"] objectForKey:@"body"] objectForKey:@"error"];
            NSLog(@"Error occured : %@", [errorInformation objectForKey:@"message"]);
        }
    }
    // Clear this token
    [FBSession.activeSession closeAndClearTokenInformation];
    // Show the user the logged-out UI
    [self userLoggedOut];
}
}

// Show the user the logged-out UI
- (void)userLoggedOut 
{
    NSLog(@"User logged out");
}

// Show the user the logged-in UI
- (void)userLoggedIn 
{
    NSLog(@"User Logged in");
    [_delegate doneLogginginToFb];
}

I have tried changing the way I authenticate but always run into the same problem... Any ideas?

Thanks

A

Andy
  • 479
  • 7
  • 20
  • did you specify the `basic_info` permission in you permissions array ? – MAB Jan 30 '14 at 22:39
  • You bet! It works (authenticates) if FB app is present so I believe the permissions are correct – Andy Jan 30 '14 at 23:11
  • Can you check in settings under facebook, if your application permission is OFF – MAB Jan 30 '14 at 23:16
  • What do you mean by off? When I go to my settings on FB I see the app there.... What am I looking for? – Andy Jan 30 '14 at 23:25
  • there must be a uiswitch on the right is it off ? – MAB Jan 30 '14 at 23:33
  • Sorry I misunderstood - well when I was first testing yes it was there and turned on - now I logged out of settings to check that it would sill work , so I'm not logged in at settings at the moment – Andy Jan 30 '14 at 23:43
  • K I just logged in - the app doesn't show up.... May have to re install from Xcode I assume – Andy Jan 30 '14 at 23:43
  • @asavu I am running into an identical issue. The currently accepted answer asks all users to reset Location & Privacy before being able to use the app, which clearly is not a solution. Were you ever able to fix this? – hunteros Jun 18 '14 at 18:39
  • @hunteros I will check my code base, but I think I re worked this code completely by following the SDK more closely, it has to do with session state changes instead of logging in and out. Let me know if you still need help with this. – Andy Jun 30 '14 at 04:07
  • @asavu since upgrading my Facebook SDK to version 3.15, I haven't seen this issue, so I'm hoping it has been resolved. Thanks for your help! – hunteros Jun 30 '14 at 18:33

3 Answers3

4

I think I did reproduce your error here, your application was denied access to Facebook account in settings (for some reason, maybe you clicked on "don't allow" ...) after the user deny the access or turn off the switch in Facebook settings, the application will not ask again for the permission and every time you will try to connect it will fail with a FBErrorCategoryUserCancelled state.

To fix this go to Settings > General > Reset, and then reset the Location & Privacy. After that run your application, you should be able to see the prompt asking for permissions.

MAB
  • 953
  • 7
  • 14
  • Didn't work - Should I remove the app from my FB settings aswell and then reset my device (simluator)? What if this happens in a "real" context – Andy Jan 31 '14 at 00:45
  • when you rest the location & privacy, your application shouldn't be in the FB settings – MAB Jan 31 '14 at 00:50
  • Okay now I removed the app from my FB and I reset as you said -. It now logs me in the instantly logs me out citing the same errors after a successful login BUT now it also says error 400, user has not authenticated the application (which makes sense casue I was never given the opportunity to) It also says session is no longer valid.... – Andy Jan 31 '14 at 01:27
  • when you try to log in, the application shows up a prompt asking for permission to access the FB account ? – MAB Jan 31 '14 at 01:45
  • No it doesn't - it just fails right away - if I'm logged into system account. When I did what you said it game me some token errors then the other errors... – Andy Jan 31 '14 at 03:45
  • @MAB I am deploying an app in testing that is also running into this exact issue. However, those who are unable to log in have confirmed that they never clicked "don't allow", and in their Settings it shows that it allows my app. Have you found any solutions that don't rely on users resetting their Location & Privacy? – hunteros Jun 18 '14 at 18:45
4

Go to Facebook developer > Your App > Settings > Basic

Click on + Add Platform

Enter your Bundle ID

Switch on Single Sign On

Save changes.

Shantanu
  • 315
  • 3
  • 13
2

I just had the same problem and it drove me crazy. I was fixed by updating the Facebook SDK to the latest version.

Not sure if this was the issue for you but I hope this helps someone.

dlp
  • 581
  • 5
  • 14