3

iOS用のFacebookSDK3.0.8を使用しています。Facebookでログインしようとすると正常に動作しますが、ログアウト後にログインしようとするとアプリがクラッシュすることがあります。

これが例外メッセージです

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

どこが悪いのか教えていただけますか?

これが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
    }
}

ViewController内のコード

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

編集:

次のコードを削除し、正常に動作するようになりました

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

ここに更新されたコードがあります

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

2 に答える 2

5

FBSession.mを見ると、のアサーションcloseは次のとおりです。

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

-closeセッションを作成したスレッド以外のスレッドから呼び出していますか?

これをさらに調べます。APIを誤用しています。あなたが作成している

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

しかし、あなたは電話しています

[FBSession openActiveSessionWithPermissions ...

これにより、まったく新しいセッションが作成されます。つまり、開いappDelegate.sessionたことがないので、閉じようとしないでください。代わりにすべきことは次のとおりです。

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

もう1つのオプションは、次のことを行うことです。

appDelegate.session = [[FBSession alloc] init];
[appDelegate.session openWithCompletionHandler: ...
于 2012-11-05T08:29:57.950 に答える
3

メインスレッドでコードを実行する必要があります

dispatch_async(dispatch_get_main_queue(), ^{
    [FBSession openActiveSessionWithPermissions:permissions
                                   allowLoginUI:YES
                              completionHandler:^(FBSession *session,
                                                  FBSessionState status,
                                                  NSError *error) {
                                                       // do something
                                                  }];
});
于 2012-11-25T07:16:13.727 に答える