即使用户登录,FBSession.activeSession.isOpen 也返回 NO [英] FBSession.activeSession.isOpen returns NO even though the user logged-in

查看:14
本文介绍了即使用户登录,FBSession.activeSession.isOpen 也返回 NO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的一个 iOS 游戏中实现了整个 iOS Facebook 登录过程.在应用程序中,您可以使用电子邮件帐户或通过 Facebook 登录.我偶尔会展示一个视图,邀请用户在使用电子邮件登录时将其帐户与 Facebook 同步.

I implemented the whole iOS Facebook login process in one of my iOS Game. In the app, you can either log-in with an email account or through Facebook. I occasionally present a view inviting the user to sync his account with Facebook if he logged-in with an email.

这是我用来确定是否应该向用户显示此视图的代码:

Here is the code I'm using to determine wheither or not I should display this view to the user :

if (FBSession.activeSession.isOpen) {
    // Present the view
} else {
    // Go somewhere else
}

起初它似乎工作得很好,但事实证明,即使用户使用 FB 登录,FBSession.activeSession.isOpen 在某些时候也会返回 NO.

At first it seemed to work great but it turns out that FBSession.activeSession.isOpen returns NO at some point even though the user is logged in with FB.

我的第一个猜测是 accessToken 过期了.我不确定这是否是一个很好的线索,因为我在我的应用程序中执行以下操作:didFinishLaunchingWithOptions:方法(如 Facebook 建议的那样):

My first guess would be that the accessToken expires. I am not sure if it's a good clue because I am doing the following in my application:didFinishLaunchingWithOptions: method (like Facebook recommends to):

if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
    // Yes, so just open the session (this won't display any UX).
    [self openSessionWithCompletionBlock:^{}];
}

所以现在我不知道如何重现或解决这个问题,知道吗?应用程序启动后令牌是否可能过期?比如用户是否将应用长时间保持在后台安静?

So now I have no idea how to reproduce or to fix this problem, any idea? Is it possible that the token expires after the app started? Like if the user keeps the app in background for a quiet long time?

推荐答案

这只是 facebook 的行为,每次启动应用程序时都需要重新打开"会话,您的猜测是对的,这是因为令牌状态,一个简单的解决方案是检查 [FBSession activeSession].isOpen 状态,如果它返回 NO 调用 openActiveSessionWithAllowLoginUI <<;是的,同样的 facebook 方法,但检查它返回的所有状态.

It's just a facebook behavior, you need to 'reopen' the session everytime you launch the app, your guess is sort of right, It's because of the token state, a simple solution is that you check the [FBSession activeSession].isOpen state and if it returns NO call the openActiveSessionWithAllowLoginUI << Yes the same facebook method, but checking all the states it returns.

如果会话先前已打开,则它使用存储的令牌重新打开会话,您不必担心登录 UI,因为它不会在会话先前打开时再次显示.

If the session was previously opened then it uses the stored token to reopen the session, you shouldn't worry about the login UI because it won't show up again as the session was previously opened.

if (![FBSession activeSession].isOpen) {
   [self connectWithFacebook];
}

- (void) connectWithFacebook { 
       // The user has initiated a login, so call the openSession method
       // and show the login UI if necessary << Only if user has never 
       // logged in or ir requesting new permissions.
       PPAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
       [appDelegate openSessionWithAllowLoginUI:YES];
}

在应用委托中

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI
{
    NSArray *permissions = @[@"any_READ_permision_you_may_need"];

    return [FBSession openActiveSessionWithReadPermissions:permissions
                                              allowLoginUI:allowLoginUI
                                         completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
                                              if (error) {
                                                  NSLog (@"Handle error %@", error.localizedDescription);
                                              } else {
                                                  [self checkSessionState:state];
                                              }
                                         }];
}

- (void) checkSessionState:(FBState)state {
    switch (state) {
        case FBSessionStateOpen:
            break;
        case FBSessionStateCreated:
            break;
        case FBSessionStateCreatedOpening:
            break;
        case FBSessionStateCreatedTokenLoaded:
            break;
        case FBSessionStateOpenTokenExtended:
            // I think this is the state that is calling
            break;
        case FBSessionStateClosed:
            break;
        case FBSessionStateClosedLoginFailed:
            break;
        default:
            break;
    }
}

这是一个快速修复并且有效,您也可以通过关闭会话来重现它,但无需使用 [[FBSession activeSession] close] 清除令牌缓存您甚至可以更改令牌缓存策略如此处所述 http://developers.facebook.com/docs/howtos/token-caching-ios-sdk/

It's a quick fix and it works, you can reproduce it also just by closing the session but without clearing the token cache using [[FBSession activeSession] close] You can even change the token cache policy as described here http://developers.facebook.com/docs/howtos/token-caching-ios-sdk/

这篇关于即使用户登录,FBSession.activeSession.isOpen 也返回 NO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆