登录设置时,iOS Facebook与signin的集成不起作用
好的,这可能是很常见的,但我在任何地方都找不到解决方案。当使用FacebookSDK作为SSO进行登录时,它在iOS模拟器中工作得非常好。然而,在实际设备上,它在一个非常具体的实例中失败:如果iPhone/iPad通过iOS中的设置登录到Facebook,它会返回“会话关闭:用户取消登录”(当收到的错误为FBErrorCategoryUserCancelled类型时,会生成此消息)。我已经尝试过这方面的每一个例子:登录设置时,iOS Facebook与signin的集成不起作用,ios,facebook,facebook-ios-sdk,Ios,Facebook,Facebook Ios Sdk,好的,这可能是很常见的,但我在任何地方都找不到解决方案。当使用FacebookSDK作为SSO进行登录时,它在iOS模拟器中工作得非常好。然而,在实际设备上,它在一个非常具体的实例中失败:如果iPhone/iPad通过iOS中的设置登录到Facebook,它会返回“会话关闭:用户取消登录”(当收到的错误为FBErrorCategoryUserCancelled类型时,会生成此消息)。我已经尝试过这方面的每一个例子: 已安装Facebook iOS应用程序并使用登录设置登录[失败] 已安装Fac
- 已安装Facebook iOS应用程序并使用登录设置登录[失败]
- 已安装Facebook iOS应用程序,但未使用登录设置登录[失败]
- 未安装Facebook iOS应用程序,登录设置[失败]
- 已安装Facebook OS应用程序,并已使用未登录的设置登录[成功]
- Facebook iOS应用已安装但未登录,设置未登录[成功]
- 未安装Facebook iOS应用程序且未登录设置[成功]
- (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
}
}
-(void)会话状态已更改:(FBSession*)会话状态:(FBSessionState)状态错误:(NSError*)错误{
//如果会话已成功打开
如果(!error&&state==FBSessionStateOpen){
NSLog(@“会话已打开”);
[[FBRequest requestForMe]startWithCompletionHandler:
^(FBRequestConnection*连接,
NSDictionary*用户,
N错误*错误){
如果(!错误){
NSString*firstName=user.first\u name;
NSString*lastName=user.last\u name;
NSString*facebookId=user.id;
NSString*email=[user objectForKey:@“email”];
[数据facebookLogin:facebookId电子邮件:电子邮件名字:名字:名字:名字];
}
}];
返回;
}
如果(状态==FBSessionStateClosed | |状态==FBSessionStateClosedLoginFailed){
//如果会话已关闭
NSLog(@“会话已关闭”);
}
//处理错误
如果(错误){
NSLog(@“错误”);
NSString*警报文本;
NSString*警报标题;
//如果错误要求使用应用程序的用户在应用程序之外执行操作以进行恢复
如果([FBErrorUtility shouldNotifyUserForError:error]==是){
alertTitle=@“出现了问题”;
alertText=[FBErrorUtility userMessageForError:error];
UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@“通知”消息:alertText委托:nil CancelButtontTitle:@“确定”其他ButtontTitles:nil];
[警报显示];
}否则{
//如果用户取消登录,则不执行任何操作
如果([FBErrorUtility errorCategoryForError:error]==FBErrorCategoryUserCancelled){
//这就是被调用的部分,就在这里!
NSLog(@“用户已取消登录”);
//处理在应用程序外部发生的会话关闭
}else if([FBErrorUtility errorCategoryForError:error]==FBErrorCategoryAuthentication重新启动会话){
alertTitle=@“会话错误”;
alertText=@“您当前的会话不再有效。请重新登录。”;
NSLog(@“%@”,alertText);
//在这里,我们将使用一般错误消息处理所有其他错误。
//我们建议您查看我们的处理错误指南以了解更多信息
// https://developers.facebook.com/docs/ios/errors/
}否则{
//从错误中获取更多错误信息
NSDictionary*errorInformation=[[error.userInfo objectForKey:@“com.facebook.sdk:ParsedJSONResponseKey”]objectForKey:@“body”]objectForKey:@“error”];
//向用户显示一条错误消息
alertTitle=@“出现了问题”;
alertText=[NSString stringWithFormat:@”请重试。\n\n如果问题仍然存在,请与我们联系并说明此错误代码:%@,[errorInformation objectForKey:@“message”];
NSLog(@“%@”,alertText);
}
}
//清除此令牌
[FBSession.activeSession closeAndClearTokenInformation];
//向用户显示已注销的UI
}
}
我知道这是一篇很长的帖子,但我认为在谈论这篇文章时,将所有信息都提供会更有益。我也有类似的问题。我认为发生的情况是,本机iOS登录机制阻塞了
@“public_profile”
权限(或者更可能是从Facebook获得的服务器响应)。若要验证,请尝试从权限数组中删除此权限
这是一个恼人的问题,因为Facebook文档声明此权限现在是强制性的。我也有类似的问题。我认为发生的情况是,本机iOS登录机制阻塞了
@“public_profile”
权限(或者更可能是从Facebook获得的服务器响应)。若要验证,请尝试从权限数组中删除此权限
这是一个恼人的问题,因为Facebook文档声明此权限现在是强制性的。奇怪的是,当我更新到3.14.0 API时,它开始正常工作。我认为3.13.1 SDK可能有问题。@regeint:很好,但在我更新到最新的Facebook SDK后,问题就消失了。奇怪的是,当我更新到3.14.0 API时,它开始正常工作。我认为3号可能有问题。