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

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

问题描述

我的iOS游戏之一实现了整个iOS Facebook登录过程。
在应用程序中,您可以使用电子邮件帐户或通过Facebook登录。我偶尔提出一个视图,邀请用户将他的帐户与Facebook同步,如果他用电子邮件登录。



这是我用于确定哪些或不是我应该向用户显示此视图:

  if(FBSession.activeSession.isOpen){
// Present视图
} else {
//去别的地方
}

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



我的第一个猜测是accessToken到期。我不知道这是否是一个很好的线索,因为我在我的应用程序中执行以下操作:didFinishLaunchingWithOptions:方法(如Facebook建议):

  if(FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded){
//是的,所以只需打开会话(这不会显示任何UX)。
[self openSessionWithCompletionBlock:^ {}];
}

所以现在我不知道如何复制或解决这个问题,任何理念?应用程序启动后,令牌可能会过期吗?喜欢用户是否在后台保持安静的时间?

解决方案

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



如果会话先前被打开,那么它使用存储的令牌重新打开会话,您不必担心登录界面,因为它不会再显示为会话以前打开。

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

- (void)connectWithFacebook {
//用户已启动登录,因此调用openSession方法
//并在必要时显示登录UI <<只有当用户从来没有
//登录或不要求新的权限。
PPAppDelegate * appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate openSessionWithAllowLoginUI:YES];
}

在应用程序委托中


$ b $ (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:
//我认为这是调用
break的状态;
case FBSessionStateClosed:
break;
case FBSessionStateClosedLoginFailed:
break;
默认值:
break;
}
}

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


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
}

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.

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?

解决方案

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.

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

And in the app delegate

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

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天全站免登陆