开发者

facebook iphone sdk logout issue!

Ive been searching for solution to what seems to be a simple problem for a while now but have had no luck. The login code is as follows.

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {

return [facebook handleOpenURL:url]; 
}

 - (void)applicationDidBecomeActive:(UIApplication *)application
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    NSString *accessToken = [prefs stringForKey:@"facebook-accessToken"];
    NSDate *expirationDate = [prefs objectForKey:@"facebook-expirationDate"];
    facebook.accessToken = accessToken;
    facebook.expirationDate = expirationDate;
    if ([facebook isSessionValid]) {
    NSLog(@"valid session");
}
else {
    NSLog(@"NOT VALID");
    [facebook authorize:nil delegate:self];
}
}

- (void)fbDidLogin {
    NSLog(@"delegate method");
    NSString *accessToken = facebook.accessToken;
    NSDate *expirationDate = facebook.expirationDate;
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:accessToken forKey:@"facebook-accessToken"];
    [prefs setObject:expirationDate forKey:@"facebook-expirationDate"];
    [prefs synchronize];
}

I have a property of Type Facebook in my rootViewController and a button to logout. I pass the facebook instance to the rootViewController and then try to log out as follows.

-(IBAction) logout {
    [facebook logout:self];

}

- (void)fbDidLogout { 
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setObject:nil forKey:@"facebook-accessToken"];
    [prefs setObject:nil forKey:@"facebook-expirationDate"];
    [prefs synchronize];
}

It kind of works..... Eg. When I do not log out I can restart the app even after closing it down properly (holding icon then pressing x) and the user will be logged in without having to do anyt开发者_开发技巧hing. But when I log out, the next time the app is started the user doesnt get a chance to re-enter the login details, instead I just get a facebook page saying "you have already authorised this app to use facebook"??

Does anyone know if I am going about this the wrong way as its really annoying me now?

Many thanks

Jules


The user might not need to re-enter his credentials again because of the single sign-on:

In the updated version of the SDK, we changed the authorization mechanism so that users no longer have to re-enter their credentials for every application on the device they want to authorize.

If the user is logged into the Facebook application, he will simply see the "you have already authorized this app to use Facebook" page.

The logout method clears all application state and makes a server request to invalidate the current access token.

Note that logging out will not revoke your application's permissions, but simply clears your application's access token. If a user that has previously logged out of your application returns, he will simply see a notification that he's logging into your application, not a notification to grant permissions.


You need to verify the session before authorize so this code still works :

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    NSString *accessToken = [prefs stringForKey:@"facebook-accessToken"];
    NSDate *expirationDate = [prefs objectForKey:@"facebook-expirationDate"];
    facebook.accessToken = accessToken;
    facebook.expirationDate = expirationDate;

if (![facebook isSessionValid]) {
    [facebook authorize:MY_APP_ID delegate:self];
}

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜