如果在调用closeAndClearTokenInformation之后调用openWithBehavior,则会导致EXC_BAD_ACCESS。无论是使用 native iOS内置对话框还是快速切换对话框之一。

我们通过首次登录到FB的代码:

if (![FBSession activeSession]) {
    #ifdef FREE_APP
        NSString* suffix = @"free";
    #else
        NSString* suffix = @"paid";
    #endif
    FBSession *session = [[[FBSession alloc] initWithAppID:@"111111111111111"
                            permissions:permissions
                        urlSchemeSuffix:suffix
                     tokenCacheStrategy:nil] autorelease];
    [FBSession setActiveSession:session];
}
else if ([FBSession activeSession].isOpen)
    [[FBSession activeSession] close];

[[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent
              completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
                                     [self sessionStateChanged:session state:state error:error];
                                 }];

我们的代码注销,之后上述代码在openWithBehavior之后失败:
[[FBSession activeSession] closeAndClearTokenInformation];

按照3.1文档中的规定,我最初使用的是openActiveSessionWithReadPermissions而不是openWithBehavior,它不会崩溃,但是从FB应用程序/Safari切回的应用程序无法正常工作。也许是因为需要后缀?如果最简单的方法是修复应用切换并返回该状态,请提出建议。

谢谢。

最佳答案

当我在5.x模拟器中运行时,我看到来自openWithBehavior的一条额外的,非常有用的错误消息,然后在源代码中进行了查找,这使事情变得更加清晰:

if (!(self.state == FBSessionStateCreated ||
      self.state == FBSessionStateCreatedTokenLoaded)) {
    // login may only be called once, and only from one of the two initial states
    [[NSException exceptionWithName:FBInvalidOperationException
                             reason:@"FBSession: an attempt was made to open an already opened or closed session"
                           userInfo:nil]
     raise];
}

我已经更改了代码,以便始终在调用openWithBehavior之前创建一个新的 session ,这似乎很高兴。

更新:
这是更新的代码,用于检查 Activity session ,然后关闭它,然后始终实例化一个新的 session ...
  - (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {


      if ([FBSession activeSession])
         [[FBSession activeSession] closeAndClearTokenInformation];

      #ifdef FREE_APP
         NSString* suffix = @"free";
      #else
         NSString* suffix = @"paid";
      #endif

      NSArray *permissions = [[NSArray alloc] initWithObjects:@"email", nil];

      FBSession *session = [[FBSession alloc] initWithAppID:mFacebookID
                                               permissions:permissions
                                           urlSchemeSuffix:suffix
                                        tokenCacheStrategy:nil];

      [FBSession setActiveSession:session];

      If (allowLoginUI == YES) {
        NSLog(@"Calling openWithBehavior");
        [[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent
                                   completionHandler:^(FBSession *session, FBSessionState state, NSError *error)
                                   {
                                      [self sessionStateChanged:session state:state error:error];
                                   }
        ];
     } else if(session.state == FBSessionStateCreatedTokenLoaded) {
        NSLog(@"Calling openWith completion handler");
        [session openWithCompletionHandler:^(FBSession *_session, FBSessionState status, NSError *error)
                                            {   [self sessionStateChanged:session state:status error:error];}
        ];
     }

     [session release];

     return true;
  }

关于ios - Facebook iOS SDK 3.1在后续调用FBSession openWithBehavior时崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13228700/

10-08 23:14