【问题标题】:IOS loses FBSession when closing appIOS在关闭应用时丢失FBSession
【发布时间】:2013-03-29 07:26:09
【问题描述】:

我很难让它工作。我有一个 IOS 应用程序,我正在尝试集成到 Facebook SDK 3.1。用户可以选择登录 Facebook,如果他们这样做我正在缓存令牌。返回的令牌到期日期提前几周,我一切正常,登录/注销,返回前台。但是,每次我关闭应用程序时,FBSession 都不会被保留。我知道这一点,因为当应用程序重新启动时,应用程序委托执行[self openSessionWithAllowLoginUI:NO] 并尝试刷新会话,但这总是返回 null。我已经关注了其他教程和其他帖子,但似乎无法在我的应用委托中看到我做错了什么。

我不知道为什么我会在应用关闭时丢失此会话。我已经附加了我的 appDelegate。

AppDelegate.m

#import "AppDelegate.h"
@implementation AppDelegate

NSString *const FBSessionStateChangedNotification=@"ro.Tag:FBSessionStateChangedNotification";


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    [self openSessionWithAllowLoginUI:NO];
    NSLog(@"%@",[[FBSession activeSession] accessToken]);

    return YES;
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    // We need to properly handle activation of the application with regards to SSO
    // (e.g., returning from iOS 6.0 authorization dialog or from fast app switching).
    [FBSession.activeSession handleDidBecomeActive];
}

/*
 * Callback for session changes.
 */
- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state) {
        case FBSessionStateOpen:
            if (!error) {
                // We have a valid session
                NSLog(@"User session found");
                NSLog(@"session %@",session);
            }
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            [FBSession.activeSession closeAndClearTokenInformation];
            break;
        default:
            break;
     }

    [[NSNotificationCenter defaultCenter]
     postNotificationName:FBSessionStateChangedNotification
     object:session];

    if (error) {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Error"
                                  message:error.localizedDescription
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
}

/*
 * Opens a Facebook session and optionally shows the login UX.
 */
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
    NSArray *permissions = [[NSArray alloc] initWithObjects:
                            @"email",
                            @"user_games_activity",
                            @"user_location",
                            @"user_likes",
                            @"user_birthday",
                            nil];

    //NSLog(@"permissions: %@",permissions);

    return [FBSession openActiveSessionWithReadPermissions:permissions
                                              allowLoginUI:allowLoginUI
                                         completionHandler:^(FBSession *session,
                                                             FBSessionState state,
                                                             NSError *error) {
                                             [self sessionStateChanged:session
                                                                 state:state
                                                                 error:error];
                                         }];
}

/*
 * If we have a valid session at the time of openURL call, we handle
 * Facebook transitions by passing the url argument to handleOpenURL
*/
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
     // attempt to extract a token from the url
     return [FBSession.activeSession handleOpenURL:url];
}
/*
  *Logout
  *
 */
 - (void) closeSession {
    [FBSession.activeSession closeAndClearTokenInformation];
 }
 @end

【问题讨论】:

  • 您是否尝试过在 openSessionWithAllowLoginUI 中打印错误。可能是您遇到了错误。
  • 在进一步研究之前尝试 Facebook iOS SDK 3.2(.1):3.2 更新修复了有关令牌保存和其他身份验证流程问题的错误。

标签: ios objective-c facebook-ios-sdk


【解决方案1】:

感谢你们两位的cmets。幸运的是,我终于能够找到问题所在。发生的事情是我已经从根视图控制器中移动了我的逻辑,但仍然调用了

[self openSessionWithAllowLoginUI:NO]

所以这意味着我连续两次调用它。一次在 appDelegate 中,再一次在根视图控制器中。第二个电话似乎清除了我的令牌,自动将我注销。一旦我能够识别这一点并从我的根视图控制器中删除该功能,一切都会按预期工作。

感谢您的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-03
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 2014-05-18
    • 1970-01-01
    • 2013-02-01
    相关资源
    最近更新 更多