返回前台时,FBSession通常无法在FBSessionStateCreatedTokenLoaded处

时间:2012-11-06 07:29:23

标签: facebook facebook-ios-sdk

我正在使用Facebook iOS SDK3.0(客户端尚未准备好转移到3.1),并且当应用程序在较长时间后返回前台时,无法让FBSession处于FBSessionStateCreatedTokenLoaded(接近一天);如果我只是将应用程序发送到后台并在一两分钟后回到前台,我就没有问题

我的代码如下,如果我应该提供进一步的信息,请告诉我。我有跨设备(4,4S,5)和iOS版本(4.3,5.0,5.1,6.0)的这个问题

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  ...   
  if (![self openSessionWithAllowLoginUI:NO]) {
    // still allow loading the public feed even though user is not logged in (the view controller will have a login button overlay)
    [[self pictureFeedVC] loadFeed];
  }
  ...
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
  DEBUGLog(@"didBecomeActive");
  DEBUGLog(@"FBSession is Open? %@",FBSession.activeSession.isOpen ? @"YES" : @"NO");

  if (FBSession.activeSession.state == FBSessionStateCreatedOpening) {
    //Dismiss login view controller
    if ([self.tabBarController modalViewController]) {
        [self.tabBarController dismissModalViewControllerAnimated:NO];
    }
    [FBSession.activeSession close];
  }
}

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
  BOOL result = NO;

  [FBSettings setLoggingBehavior:[NSSet setWithObjects:FBLoggingBehaviorSessionStateTransitions,nil]];
  NSArray *permissions = [NSArray arrayWithObjects:@"publish_actions", @"email", nil];

  FBSession *session = [[FBSession alloc] initWithAppID:nil permissions:permissions urlSchemeSuffix:FB_URL_SCHEME_SUFFIX tokenCacheStrategy:nil];

  if (allowLoginUI || (session.state == FBSessionStateCreatedTokenLoaded)) {
    [FBSession setActiveSession:session];
    [session openWithCompletionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
        [self sessionStateChanged:session state:state error:error];
    }];
    result = session.isOpen;
  }
  return result;
}

- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState)state error:(NSError *)error
{
  DEBUGLog(@"changed FB state");
  switch (state) {
    case FBSessionStateOpen:
      if (!error) {
        DEBUGLog(@"FBSessionStateOpen");
        [[self pictureFeedVC] loadFeed];
      }
    break;
    case FBSessionStateClosed:
      DEBUGLog(@"FBSessionStateClosed");
    case FBSessionStateClosedLoginFailed:
      DEBUGLog(@"FBSessionStateClosedLoginFailed");
      [session closeAndClearTokenInformation];
      [self.tabBarController showFBLoginView];
      break;
    default:
      break;
    }
}

我遇到问题,在后台延长期后,DEBUGLog(@"FBSession is Open? %@",FBSession.activeSession.isOpen ? @"YES" : @"NO");中的applicationDidBecomeActive:会打印NO

更多信息:这个应用程序完全依赖于Facebook登录,它使用Facebook用户ID来识别用户,因此我需要通过Facebook访问令牌进行大量操作,以便服务器可以获得Facebook用户ID,即使很多这些动作不需要api调用facebook(当然除了从访问令牌获取用户ID)。我的另一个问题是我是否需要主动做一些事情来刷新令牌或做一些事情让FBSession保持打开(如果是这样,怎么样?),这就是为什么FBSession.activeSession可能不处于打开状态的原因我回到前台了?

提前多多谢谢!

0 个答案:

没有答案