FaceBook SDK,FBSession动作

时间:2014-06-02 19:00:28

标签: ios objective-c facebook facebook-graph-api

我正在使用集成FB SDK的iOS应用程序。 现在我跟随FB的示例代码,以及类FBSession的工作方式对我来说还不清楚。 我完全理解登录过程中唯一的问题是在注销过程中。

在FBLoginCustomUISample示例代码中,有一种登录/退出用户的方法。

- (IBAction)buttonTouched:(id)sender
{
  // If the session state is any of the two "open" states when the button is clicked
  if (FBSession.activeSession.state == FBSessionStateOpen
      || FBSession.activeSession.state == FBSessionStateOpenTokenExtended) {

    // Close the session and remove the access token from the cache
    // The session state handler (in the app delegate) will be called automatically
    [FBSession.activeSession closeAndClearTokenInformation];

  // If the session state is not any of the two "open" states when the button is clicked
  } else {
    // Open a session showing the user the login UI
    // You must ALWAYS ask for public_profile permissions when opening a session
    [FBSession openActiveSessionWithReadPermissions:@[@"public_profile"]
                                       allowLoginUI:YES
                                  completionHandler:
     ^(FBSession *session, FBSessionState state, NSError *error) {

       // Retrieve the app delegate
       AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
       // Call the app delegate's sessionStateChanged:state:error method to handle session state changes
       [appDelegate sessionStateChanged:session state:state error:error];
     }];
  }
}

每当我登录应用程序时,一切顺利,登录警报视图就会出现。 现在当我再次按下按钮退出时,它会调用

[FBSession.activeSession closeAndClearTokenInformation];

但在完成此方法之后,代码会跳到if-else语句的第二部分,然后执行

   AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
   // Call the app delegate's sessionStateChanged:state:error method to handle session state changes
   [appDelegate sessionStateChanged:session state:state error:error];

sessionStateChanged:state:error:是一种处理会话状态的方法(我不想添加它,因为它很长,但可以在示例代码中查看)。 我的问题是为什么它会转到if-else

的第二部分

谢谢,

1 个答案:

答案 0 :(得分:0)

From documentation:

handler 
Many applications will benefit from notification when a session becomes invalid or undergoes other state transitions. If a block is provided, the FBSession object will call the block each time the session changes state.

所以,你不应该删除方法sessionStateChanged...,因为有必要获得关于你的FBSession的实际信息,并且无论如何都会调用这个方法。