4

我很难让这个工作。我有一个 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
4

1 回答 1

3

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

[self openSessionWithAllowLoginUI:NO]

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

谢谢你的帮助。

于 2013-04-01T04:11:02.323 回答