24

我正在使用 Facebook SDK 3.1.1 在我的 iOS 应用程序中实现 FB Connect。在简单的情况下,无论是新的 FB 集成(在 iOS 上登录)还是通过 Web 视图回退到正常授权(在这两种情况下我都没有安装本机 Facebook 应用程序),这都可以正常工作。当我在 iOS 级别切换帐户时会出现问题。注销并使用不同的 FB 用户帐户登录。

要登录/授权,我执行:

[FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:allowLoginUI
                                     completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
                                         [self sessionStateChanged:session state:state error:error];
                                     }];

如果然后每次都得到一个 FBSessionStateClosedLoginFailed ,即使我在closeAndClearTokenInformation达到该状态时执行一个:

- (void)sessionStateChanged:(FBSession *)session
                  state:(FBSessionState) state
                  error:(NSError *)error
{
    NSLog(@"Session State Changed: %u", [[FBSession activeSession] state]);
    switch (state) {
        case FBSessionStateOpen:
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            NSLog(@"FBSessionStateClosedLoginFailed ERROR: %@", [error description]);
            [[FBSession activeSession] closeAndClearTokenInformation];
            break;
        default:
            break;
}

但是,每次重试时我都会收到相同的状态。我的日志说以下内容:

FBSDKLog: FBSession **INVALID** transition from FBSessionStateCreated to FBSessionStateClosed
FBSDKLog: FBSession transition from FBSessionStateCreated to FBSessionStateCreatedOpening 
FBSDKLog: FBSession transition from FBSessionStateCreatedOpening to FBSessionStateClosedLoginFailed Session State Changed: 257
FBSessionStateClosedLoginFailed TOKEN: (null)
FBSessionStateClosedLoginFailed ERROR: Error Domain=com.facebook.sdk Code=2 "The operation couldn’t be completed. (com.facebook.sdk error 2.)" UserInfo=0xb24cc20 {com.facebook.sdk:ErrorLoginFailedReason=com.facebook.sdk:ErrorLoginFailedReason}

任何人都可以重现这个或知道问题可能出在哪里吗?

4

6 回答 6

38

另一个答案提供了一种手动将设备与服务器重新同步的方法。我定义了一个fbRsync调用此代码的方法。确保#import <Accounts/Accounts.h>在您的实现文件中,然后定义此方法:

-(void)fbResync
{
    ACAccountStore *accountStore;
    ACAccountType *accountTypeFB;
    if ((accountStore = [[ACAccountStore alloc] init]) && (accountTypeFB = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook] ) ){

    NSArray *fbAccounts = [accountStore accountsWithAccountType:accountTypeFB];
    id account;
    if (fbAccounts && [fbAccounts count] > 0 && (account = [fbAccounts objectAtIndex:0])){

    [accountStore renewCredentialsForAccount:account completion:^(ACAccountCredentialRenewResult renewResult, NSError *error) {
        //we don't actually need to inspect renewResult or error.
        if (error){

        }
    }];
}

}

然后我调用fbResyncifopenActiveSessionWithReadPermissions产生错误:

[FBSession openActiveSessionWithReadPermissions:permissions
                                   allowLoginUI:YES
                              completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
     if(error)
     {
         NSLog(@"Session error");
         [self fbResync];
         [NSThread sleepForTimeInterval:0.5];   //half a second
         [FBSession openActiveSessionWithReadPermissions:permissions
                                            allowLoginUI:YES
                                       completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
            [self sessionStateChanged:session state:state error:error];
                                       }];

     }
     else
         [self sessionStateChanged:session state:state error:error];
 }];

半秒的延迟可能是不必要的,但它目前让我放心。

这似乎解决了我的问题。我现在可以在 Facebook 帐户之间切换并能够登录。耶!

于 2012-10-12T16:06:08.083 回答
12

I had the same problem. Check that your FB App is enabled in Settings -> Facebook. Mine was disabled (even though I don't remember disabling it) and once I enabled it, it was fixed.

In my test process, I've added and removed my FB App several times from my FB Account, which is linked with my iPhone. It may explain why, magically, my app was disabled.

于 2013-03-08T23:42:51.570 回答
3

在带有 fb sdk 3.1.1 的 ios 6 中。请在“[FBSessio openActiveSessionWithReadPermissions...”方法中将权限参数作为“nil 或电子邮件”传递。在这里,我的代码效果很好。

#define IOS_NEWER_OR_EQUAL_TO_6 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] >= 6.0 )

-(void)showFBLogin
{
    [FBSession.activeSession closeAndClearTokenInformation];

    NSArray *permissions = [NSArray arrayWithObjects:@"email, publish_actions, publish_stream", nil];

#ifdef IOS_NEWER_OR_EQUAL_TO_6
    permissions = nil; or NSArray *permissions = [NSArray arrayWithObjects:@"email",nil];
#endif

    NSLog(@"\npermissions = %@", permissions);
    [FBSession openActiveSessionWithReadPermissions:permissions
                                       allowLoginUI:YES
                                  completionHandler:
     ^(FBSession *session,
       FBSessionState state, NSError *error) {
         NSLog(@"\nfb sdk error = %@", error);
         switch (state) {
             case FBSessionStateOpen:
                 [[FBRequest requestForMe] startWithCompletionHandler:
                  ^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
                      if (!error) {
                          //success
                      } 
                  }];
                 break;
             case FBSessionStateClosed:
                 //need to handle
                 break;
             case FBSessionStateClosedLoginFailed:
                 //need to handle
                 break;
             default:
                 break;
         }
     }];
}
于 2013-02-07T07:42:24.630 回答
1


我在 iOS7.1 的 3.1.3 FB SDK 上遇到了同样的问题。我在这里找到了解决方案。希望有帮助!!
我在这里尝试了所有答案。
@loganathan 无权限,@ill_always_be_a_warriors fbResync。
所有这些都不适合我。

但是我发现当我在 iPhone 7.1 模拟器中启动相同的代码时它会运行良好
(没有 SSO“单点登录”)

相同的代码在旧版本的 iOS FB SDK 上运行良好(不确定哪个版本,但不是 3.13)
(但没有SSO 将在尝试登录时显示)

所以,我尝试重新编写一个示例问题。我在这里发现了几个不同的地方。
1. https://developers.facebook.com/docs/ios/getting-started添加新的 FacebookDisplayName
2. 我为 iTune connect 修改了我的 BundleID,但我没有将它更新回 Faceboook App Dev


修改这些设置后,它可以在我的具有 SSO 功能的 iOS 应用程序上运行。
希望有帮助!!

于 2014-03-24T12:18:35.100 回答
0

您是否尝试过 RenewSystemCredential 方法?看看这个帖子:

Facebook iOS SDK extendAccessToken 不工作 - 未调用 fbDidExtendToken

于 2013-05-21T12:27:03.370 回答
0

我得到了同样的错误。但是我使用了@ill_always_be_a_warriors 方法:-(void)fbResync但对我不起作用。

我终于发现这是我的权限问题,我删除了offline_access权限,它有效,希望它对某人有所帮助。

于 2014-01-21T08:00:12.593 回答