好的,这可能是非常常见的,但我无法在任何地方找到解决方案。当使用Facebook SDK处理作为SSO登录时,它在iOS模拟器中运行良好。但是,在实际设备上,它在一个非常具体的实例中失败:如果iPhone / iPad通过iOS中的设置登录到Facebook,则会返回“会话已关闭:用户取消登录”(错误时会生成此消息)收到的是FBErrorCategoryUserCancelled类型)。我试过这个的每一个例子:
这一切都与登录Facebook的设置有关。现在我知道其他人的应用程序可以让Facebook与登录的设置集成,所以我错过了什么?这是我的登录代码:
- (IBAction)loginWithFacebook:(id)sender {
if (FBSession.activeSession.state == FBSessionStateOpen || FBSession.activeSession.state == FBSessionStateOpenTokenExtended) {
[FBSession.activeSession closeAndClearTokenInformation];
}
[FBSession openActiveSessionWithReadPermissions:@[@"basic_info",@"public_profile",@"email"]
allowLoginUI:YES
completionHandler: ^(FBSession *session, FBSessionState state, NSError *error) {
// Retrieve the app delegate
AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
// Call the app delegate's sessionStateChanged:state:error method to handle session state changes
[appDelegate sessionStateChanged:session state:state error:error];
}];
}
这是我的代码,它从AppDelegate处理所有这些:
- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error {
// If the session was opened successfully
if (!error && state == FBSessionStateOpen){
NSLog(@"Session opened");
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *user,
NSError *error) {
if (!error) {
NSString *firstName = user.first_name;
NSString *lastName = user.last_name;
NSString *facebookId = user.id;
NSString *email = [user objectForKey:@"email"];
[Data facebookLogin:facebookId email:email firstName:firstName lastName:lastName];
}
}];
return;
}
if (state == FBSessionStateClosed || state == FBSessionStateClosedLoginFailed){
// If the session is closed
NSLog(@"Session closed");
}
// Handle errors
if (error){
NSLog(@"Error");
NSString *alertText;
NSString *alertTitle;
// If the error requires people using an app to make an action outside of the app in order to recover
if ([FBErrorUtility shouldNotifyUserForError:error] == YES){
alertTitle = @"Something went wrong";
alertText = [FBErrorUtility userMessageForError:error];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Notice" message: alertText delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
} else {
// If the user cancelled login, do nothing
if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
// This is the part that gets called, right here!
NSLog(@"User cancelled login");
// Handle session closures that happen outside of the app
} else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession){
alertTitle = @"Session Error";
alertText = @"Your current session is no longer valid. Please log in again.";
NSLog(@"%@",alertText);
// Here we will handle all other errors with a generic error message.
// We recommend you check our Handling Errors guide for more information
// https://developers.facebook.com/docs/ios/errors/
} else {
//Get more error information from the error
NSDictionary *errorInformation = [[[error.userInfo objectForKey:@"com.facebook.sdk:ParsedJSONResponseKey"] objectForKey:@"body"] objectForKey:@"error"];
// Show the user an error message
alertTitle = @"Something went wrong";
alertText = [NSString stringWithFormat:@"Please retry. \n\n If the problem persists contact us and mention this error code: %@", [errorInformation objectForKey:@"message"]];
NSLog(@"%@",alertText);
}
}
// Clear this token
[FBSession.activeSession closeAndClearTokenInformation];
// Show the user the logged-out UI
}
}
我知道这是一个很长的帖子,但我认为在讨论这个问题时提供所有信息会更有益。
答案 0 :(得分:4)
我有类似的问题。我认为正在发生的事情是本机iOS登录机制在@"public_profile"
权限上窒息(或者更可能是服务器对它的响应,它来自Facebook)。要进行验证,请尝试从权限数组中删除此权限。
这是一个恼人的问题,因为Facebook文档声明此权限现在是强制性的。