0

我目前正在开发一个使用 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];
}

但它仍然要求我授权。用户每次登录都看到授权窗口非常烦人。有人对此有任何想法吗?

提前致谢!

4

3 回答 3

1

我发现了我的错误!

登录时,我将密钥另存为,FBAccessTokenKey但我尝试读取FBAccessToken.

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

于 2012-07-07T13:55:28.410 回答
1

确保NSUserDefaults您使用的字符串accessToken不是nil并且您正确保存它。

于 2012-07-07T13:18:27.273 回答
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 中做这些垃圾!

于 2012-07-07T13:20:39.687 回答