Facebook iOS SDK 3.1在随后调用FBSession openWithBehavior时崩溃

leo*_*ntx 7 sdk facebook ios

如果我们在调用closeAndClearTokenInformation之后调用openWithBehavior,则会导致EXC_BAD_ACCESS.无论是使用本机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];
                                 }];
Run Code Online (Sandbox Code Playgroud)

我们的注销代码,之后上面的代码在openWithBehavior之后失败:

[[FBSession activeSession] closeAndClearTokenInformation];
Run Code Online (Sandbox Code Playgroud)

我最初使用openActiveSessionWithReadPermissions而不是openWithBehavior,如3.1文档中规定的那样,它不会崩溃,但是从FB app/Safari切换回的应用程序不起作用.也许是因为需要有后缀?如果最容易修复应用程序切换并返回到那个,请告知.

谢谢.

leo*_*ntx 7

当我在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];
}
Run Code Online (Sandbox Code Playgroud)

我已经改变了我的代码,在调用openWithBehavior之前总是创建一个新的会话,看起来很开心.

更新:这是更新的代码,它检查活动会话,然后关闭它,然后始终实例化一个新会话...

  - (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;
  }
Run Code Online (Sandbox Code Playgroud)