关闭应用程序时,IOS会丢失FBSession

时间:2013-03-29 07:26:09

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

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

我正在敲我的头,因为我在app app关闭时失去了这个会话。我已经附上了我的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

1 个答案:

答案 0 :(得分:3)

感谢您的评论。幸运的是,我终于能够追查这个问题了。发生的事情是我从根视图控制器移动了我的逻辑,但仍然调用了

[self openSessionWithAllowLoginUI:NO]

所以这意味着我连续两次打电话。一旦进入appDelegate并再次进入根视图控制器。第二个电话似乎清除我的令牌自动登出我。一旦我能够识别出这个并从我的根视图控制器中删除功能,一切都按预期工作。

感谢您的帮助。