关闭应用后,Facebook会话无效

时间:2012-07-07 13:14:35

标签: ios facebook single-sign-on

我目前正在开发一款使用Facebook登录SSO的应用。我在他们的iOS开发者网站上关注了这个教程,现在我有了一个有效的登录和注销系统:)

但是,每当我启动应用程序时,会话都无效(不再),并且它要求我授权该应用程序。即使在我关闭应用程序之后,会话是否可能保持有效,我将如何实现?我已经尝试过以下代码:

// Restore previously saved Facebook credentials (If any)
delegate.facebook.accessToken    = [[NSUserDefaults standardUserDefaults] stringForKey:@"FBAccessToken"];
delegate.facebook.expirationDate = (NSDate *) [[NSUserDefaults standardUserDefaults] objectForKey:@"FBExpirationDate"];

// Trigger SSO Facebook authentication if required
if ([delegate.facebook isSessionValid] == NO) {
    [delegate.facebook authorize:nil];
} else {
    [self fbLoginWithUsername:username andPassword:password];
}

但它仍然要求我授权。用户每次登录时都会看到授权窗口,这非常烦人。有没有人对此有所了解?

提前致谢!

3 个答案:

答案 0 :(得分:1)

确保您用于NSUserDefaults的{​​{1}}字符串不是accessToken,并且您正确保存它。

答案 1 :(得分:1)

我更倾向于使用单例样式扩展Facebook类并实现:

static Facebook *shared = nil;

    + (Facebook *)shared {
        @synchronized(self) {
            if(shared == nil)
                shared = [[self alloc] init];
        }
        return shared;
    }

    - (id)init {
        if ((self = [self initWithAppId:kFacebookAppID andDelegate:self])) {
            [self addSessionData];
        }
        return self;
    }

    -(void)addSessionData{
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        if ([defaults objectForKey:kFBAccessTokenKey] && [defaults objectForKey:kFBExpirationDateKey]) {
            self.accessToken = [defaults objectForKey:kFBAccessTokenKey];
            self.expirationDate = [defaults objectForKey:kFBExpirationDateKey];
        }
    }

    - (void)authorize {

        if (![self isSessionValid]) {
            NSArray *permissions =  [NSArray arrayWithObjects:@"email", @"user_about_me", nil];
            [self authorize:permissions];
        }
    }

    - (void)fbDidLogin {

        NSLog(@"User is logged in to Facebook");

        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:[self accessToken] forKey:kFBAccessTokenKey];
        [defaults setObject:[self expirationDate] forKey:kFBExpirationDateKey];
        [defaults synchronize];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"FBDidLogin" object:self];
    }

    - (void)fbDidNotLogin:(BOOL)cancelled {

        if (cancelled) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"FBLoginCancelled" object:self];
        } else {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"FBLoginFailed" object:self];
        }

        [[NSNotificationCenter defaultCenter] postNotificationName:@"FBDidNotLogin" object:self];
    }

    - (void)fbDidLogout {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults removeObjectForKey:kFBAccessTokenKey];
        [defaults removeObjectForKey:kFBExpirationDateKey];
        [defaults synchronize];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"FBDidLogout" object:self];
        NSLog(@"Logged out of Facebook");
    }

    - (void)fbDidExtendToken:(NSString*)accessToken expiresAt:(NSDate*)expiresAt{
        NSLog(@"Facebook access token extended");
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:accessToken forKey:kFBAccessTokenKey];
        [defaults setObject:expiresAt forKey:kFBExpirationDateKey];
        [defaults synchronize];
    }


    - (void)fbSessionInvalidated{

    }

我知道代码有效。幸运的是,我们不必在iOS6中做这个垃圾!

答案 2 :(得分:1)

我发现了我的错误!

当我登录时,我将密钥保存为FBAccessTokenKey,但我尝试阅读FBAccessToken

有点傻,我知道;)现在它按预期工作了!