简体   繁体   English

Facebook iOS SDK,FBSession异常,***断言失败 - [FBSession close]

[英]Facebook iOS SDK , exception against FBSession, *** Assertion failure in -[FBSession close]

I'm using facebook sdk 3.0.8 for ios. 我正在使用facebook sdk 3.0.8 for ios。 when i try to login with facebook it works fine but sometimes when i try to login after logging out then app is being crashed. 当我尝试使用Facebook登录时,它工作正常,但有时当我在登出后尝试登录时,应用程序正在崩溃。

here is exception message 这是异常消息

*** Assertion failure in -[FBSession close], /Users/jacl/src/ship/ios-sdk/src/FBSession.m:342

can you please tell me where i'm going wrong? 你能告诉我哪里出错了吗?

Here is code inside AppDelegate 这是AppDelegate中的代码

    - (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    // attempt to extract a token from the url
    return [FBSession.activeSession handleOpenURL:url]; 
}


- (void)applicationWillTerminate:(UIApplication *)application {

    [self.session close];
}

#pragma mark Template generated code


// FBSample logic
// It is possible for the user to switch back to your application, from the native Facebook application, 
// when the user is part-way through a login; You can check for the FBSessionStateCreatedOpenening
// state in applicationDidBecomeActive, to identify this situation and close the session; a more sophisticated
// application may choose to notify the user that they switched away from the Facebook application without
// completely logging in
- (void)applicationDidBecomeActive:(UIApplication *)application {
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */


    // FBSample logic
    // this means the user switched back to this app without completing a login in Safari/Facebook App
    if (self.session.state == FBSessionStateCreatedOpening) {
        // BUG: for the iOS 6 preview we comment this line out to compensate for a race-condition in our
        // state transition handling for integrated Facebook Login; production code should close a
        // session in the opening state on transition back to the application; this line will again be
        // active in the next production rev
        //[self.session close]; // so we close our session and start over
    }
}

Code Inside View Controller 代码内部视图控制器

-(IBAction)connectWithFacebook{
         DemoAppDelegate *appDelegate = (TotallyCuteAppDelegate *) [[UIApplication sharedApplication]delegate];
        if (!appDelegate.session.isOpen && (appDelegate.session.state != FBSessionStateCreated))
        {
            appDelegate.session = [[FBSession alloc] init];
        }

        NSArray *permissions = [[NSArray alloc] initWithObjects:
                                @"publish_actions",
                                @"email",
                                nil];
        //app crashes here
        [FBSession openActiveSessionWithPermissions:permissions allowLoginUI:YES 
                                  completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
                                      if (session.isOpen) 
                                      {
                                        NSLog(@"LoginVC->Session is open");
                                        appDelegate.session=session; 
                                        [userDefaults setObject:appDelegate.session.accessToken forKey:@"facebook_token"];
                                        [userDefaults setObject:paramFBId forKey:@"facebook_id"];


                                      }
                                      else
                                      {
                                          NSLog(@"LoginVC->Session is not open");
                                      }
                                  }//end completionHandler
         ];

}




-(IBAction)logout:(id)sender{
    DemoAppDelegate *appDelegate = (TotallyCuteAppDelegate *) [[UIApplication sharedApplication]delegate]; 

    if (appDelegate.session.isOpen) {
        [appDelegate.session closeAndClearTokenInformation];

        [[NSUserDefaults userDefaults] removeObjectForKey:@"facebook_id"];
        [[NSUserDefaults userDefaults] removeObjectForKey:@"facebook_token"];
      } 
}

Edit: 编辑:

removed the following code and now it works fine 删除了以下代码,现在它工作正常

  if (!appDelegate.session.isOpen && (appDelegate.session.state != FBSessionStateCreated))
            {
                appDelegate.session = [[FBSession alloc] init];
            }

here is updated code 这是更新的代码

 -(IBAction)connectWithFacebook{
        if ([InternetChecker isConnected]) 
        {
            DemoAppDelegate *appDelegate = (TotallyCuteAppDelegate *) [[UIApplication sharedApplication]delegate];

    /* Removed following if block
           if (!appDelegate.session.isOpen && (appDelegate.session.state != FBSessionStateCreated))
            {
                appDelegate.session = [[FBSession alloc] init];
            }
    */
            NSArray *permissions = [[NSArray alloc] initWithObjects:
                                    @"publish_actions",
                                    @"email",
                                    nil];

            [FBSession openActiveSessionWithPermissions:permissions allowLoginUI:YES 
                                      completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
                                          if (session.isOpen) 
                                          {
                                            NSLog(@"LoginVC->Session is open");
                                            appDelegate.session=session; 
                                            [userDefaults setObject:appDelegate.session.accessToken forKey:@"facebook_token"];
                                            [userDefaults setObject:paramFBId forKey:@"facebook_id"];


                                          }
                                          else
                                          {
                                              NSLog(@"LoginVC->Session is not open);
                                          }
                                      }//end completionHandler
             ];
        } 
    }

Looking at FBSession.m, the assertion in close is as follows: 查看FBSession.m, close的断言如下:

NSAssert(self.affinitizedThread == [NSThread currentThread], @"FBSession: should only be used from a single thread");

Are you calling -close from a thread other than the one you created the session on? 你打电话-close从比你在创建该会话的另一个线程?

Looking into this further. 进一步研究这个问题。 You're misusing the API. 你误用了API。 You are creating 你正在创造

appDelegate.session = [[FBSession alloc] init]; 

but then you are calling 但是你正在打电话

[FBSession openActiveSessionWithPermissions ...

which creates an entirely new session. 这会创建一个全新的会话。 Meaning you never opened appDelegate.session , and therefore you should not try to close it. 这意味着您从未打开appDelegate.session ,因此您不应该尝试关闭它。 What you should be doing instead is the following: 您应该做的是以下内容:

[FBSession openActiveSessionWithPermissions ...
appDelegate.session = [FBSession activeSession];

Another option is to just do: 另一种选择是:

appDelegate.session = [[FBSession alloc] init];
[appDelegate.session openWithCompletionHandler: ...

You should perform your code on the main thread 您应该在主线程上执行代码

dispatch_async(dispatch_get_main_queue(), ^{
    [FBSession openActiveSessionWithPermissions:permissions
                                   allowLoginUI:YES
                              completionHandler:^(FBSession *session,
                                                  FBSessionState status,
                                                  NSError *error) {
                                                       // do something
                                                  }];
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM