Facebook Login on iOS works in 3 ways:
- Try to log in to the native iOS login, if the user has this option configured;
- Try to log in to the Facebook application if it is installed;
- Open the Facebook site in Safari for the user to log in.
My problem only happens in the first case. If the user tries to log in using the native iOS login this returns FBErrorCategoryUserCancelled. I already checked the permission in the iOS Privacy area and it's OK. My app is already out of sandbox mode and AppID confirms with the App ID on Facebook.
The following is the login code:
- (void)viewDidLoad
{
[super viewDidLoad];
_flag = 0;
UIBarButtonItem *botaoFechar = [[UIBarButtonItem alloc] initWithTitle:@"Fechar" style:UIBarButtonItemStyleDone target:self action:@selector(fechar)];
self.navigationItem.rightBarButtonItem = botaoFechar;
delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
HUD = [[MBProgressHUD alloc] initWithView:self.view];
HUD.delegate = self;
[HUD removeFromSuperViewOnHide];
[self.view addSubview:HUD];
FBLoginView *loginView = [[FBLoginView alloc] initWithFrame:CGRectMake(0, 100, 120, 60)];
loginView.readPermissions = @[@"email", @"public_profile", @"user_friends", @"user_birthday", @"user_location"];
loginView.delegate = self;
[self.view addSubview:loginView];
// Do any additional setup after loading the view from its nib.
}
#pragma mark - Facebook
- (void)loginView:(FBLoginView *)loginView handleError:(NSError *)error {
NSString *alertMessage, *alertTitle;
// If the user should perform an action outside of you app to recover,
// the SDK will provide a message for the user, you just need to surface it.
// This conveniently handles cases like Facebook password change or unverified Facebook accounts.
if ([FBErrorUtility shouldNotifyUserForError:error]) {
alertTitle = @"Facebook error";
alertMessage = [FBErrorUtility userMessageForError:error];
// This code will handle session closures that happen outside of the app
// You can take a look at our error handling guide to know more about it
// https://developers.facebook.com/docs/ios/errors
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession) {
alertTitle = @"Session Error";
alertMessage = @"Your current session is no longer valid. Please log in again.";
// If the user has cancelled a login, we will do nothing.
// You can also choose to show the user a message if cancelling login will result in
// the user not being able to complete a task they had initiated in your app
// (like accessing FB-stored information or posting to Facebook)
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
NSLog(@"user cancelled login %@", error);
// For simplicity, this sample handles other errors with a generic message
// You can checkout our error handling guide for more detailed information
// https://developers.facebook.com/docs/ios/errors
} else {
alertTitle = @"Something went wrong";
alertMessage = @"Please try again later.";
NSLog(@"Unexpected error:%@", error);
}
if (alertMessage) {
[[[UIAlertView alloc] initWithTitle:alertTitle
message:alertMessage
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}
}
-(void)loginViewShowingLoggedInUser:(FBLoginView *)loginView
{
[HUD show:YES];
}
- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
user:(id<FBGraphUser>)user {
if(_flag == 0){
[self loginComUsuario:user];
_flag++;
}
}
The returned log is:
2014-08-25 14:40:08.065 ShowApp[360:60b] user cancelled login Error Domain=com.facebook.sdk Code=2 "The operation couldn’t be completed. (com.facebook.sdk error 2.)" UserInfo=0x146b2410 {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: 0x146ad8d0, state: FBSessionStateClosedLoginFailed, loginHandler: 0x0, appID: 600192863435061, urlSchemeSuffix: , tokenCachingStrategy:<FBSessionTokenCachingStrategy: 0x14562020>, expirationDate: (null), refreshDate: (null), attemptedRefreshDate: 0001-12-30 00:00:00 +0000, permissions:(null)>}