1

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

我的代码如下,如果我应该提供更多信息,请告诉我;我在设备(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");inapplicationDidBecomeActive:将打印一个NO

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

提前非常感谢!

4

0 回答 0