登录设置后,iOS Facebook 与登录的集成不起作用

标签 ios facebook facebook-ios-sdk

好吧,这可能是很常见的事情,但我无法在任何地方找到解决方案。当使用 Facebook SDK 处理作为 SSO 的登录时,它在 iOS 模拟器中运行良好。然而,在实际设备上,它会在一个非常具体的实例中失败:如果 iPhone/iPad 通过 iOS 中的设置登录到 Facebook,它会返回“ session 关闭:用户取消登录”(错误时生成此消息received 是 FBErrorCategoryUserCancelled 类型)。我已经尝试了每个实例:

  • 已安装 Facebook iOS 应用程序并使用登录设置登录 [FAIL]
  • 已安装 Facebook iOS 应用程序但未登录设置已登录 [FAIL]
  • Facebook iOS 应用未安装且设置已登录[FAIL]
  • 已安装并登录 Facebook 操作系统应用,但未登录设置[SUCCESS]
  • 已安装 Facebook iOS 应用但未登录且设置未登录[SUCCESS]
  • Facebook iOS 应用未安装且设置未登录[SUCCESS]

这一切都与登录 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
    }
}

我知道这是一篇很长的文章,但我认为在讨论这个问题时提供所有可用信息会更有帮助。

最佳答案

我遇到了类似的问题。我认为正在发生的事情是 native iOS 登录机制在 @"public_profile" 权限上阻塞(或者更可能是服务器对它的响应是从 Facebook 获得的)。要验证,请尝试从您的权限数组中删除此权限。

这是一个恼人的问题,因为 Facebook 文档声明此权限现在是强制性的。

关于登录设置后,iOS Facebook 与登录的集成不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23413338/

相关文章:

ios - 如何在 "Open with"中获取我的应用程序

ios - 网站不会在 iOS 版 Facebook 应用程序中打开,也不会重定向到 Safari

php - 简单的 "facebook.php"不工作

ios - 多进程iOS应用程序

ios - CLCircularRegion 获取1英里半径内的随机点

javascript - jQuery 点击事件似乎有点晚了

ios - SQL 查询根据性别获取用户的 Facebook 好友列表

ios - 如何使用 Facebook iOS SDK 3.14.1 以编程方式创建 Facebook 测试用户

ios - iOS 上的 POST 请求 - 不起作用

iphone - 所以 "lame"- 如何从 float = 0.39824702 只是 float = 0.398?