【问题标题】:Facebook iOS SDK 3.1 crashes on subsequent call to FBSession openWithBehaviorFacebook iOS SDK 3.1 在随后调用 FBSession openWithBehavior 时崩溃
【发布时间】:2012-11-05 08:52:12
【问题描述】:

如果我们在调用 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];
                                 }];

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

[[FBSession activeSession] closeAndClearTokenInformation];

我最初使用的是 openActiveSessionWithReadPermissions 而不是 openWithBehavior,如 3.1 文档中所述,它不会崩溃,但从 FB 应用程序/Safari 切换回的应用程序不起作用。也许是因为需要后缀?如果修复应用程序切换并返回最简单,请告知。

谢谢。

【问题讨论】:

    标签: ios facebook sdk


    【解决方案1】:

    当我在 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 之前始终创建一个新会话,这似乎很开心。

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

      - (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;
      }
    

    【讨论】:

    • ,我不知道在哪里以及为什么要编写这段代码。我在 openWithBehaviour 之前写了它,但它仍然在 openWithBehaviour 中崩溃
    • 上面的第一个 sn-p 来自抛出错误的 facebook 类。我只是将它包括在内以添加正在发生的事情的上下文。在添加对活动会话的检查并关闭它之后,我继续添加我的代码,然后总是创建一个新的会话。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 2012-09-18
    • 1970-01-01
    相关资源
    最近更新 更多