iVipS
iVipS

Reputation: 1485

Facebook logout not working properly through the Facebook Connect iPhone API

I am executing the following code to logout of Facebook from my iPhone application:

if ([mFacebook isSessionValid]) {
    [mFacebook logout];
}

This code runs successfully and the delegate is called after this in which I am clearing the access token:

- (void)fbDidLogout 
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:@"FBAccessTokenKey"];
    [defaults removeObjectForKey:@"FBExpirationDateKey"];
    [defaults synchronize];
}

But when I again log in to Facebook, it does not ask for username and password.

What am I doing wrong?

Upvotes: 0

Views: 3251

Answers (5)

Luca S.
Luca S.

Reputation: 94

Try to log off from the Facebook application. It might just take the token from the Facebook application.

Upvotes: 0

Dinesh Raja
Dinesh Raja

Reputation: 8501

Try this code:

- (void) fbDidLogout {
    // Remove saved authorization information if it exists
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"FBAccessTokenKey"]) {
        [defaults removeObjectForKey:@"FBAccessTokenKey"];
        [defaults removeObjectForKey:@"FBExpirationDateKey"];
        [defaults synchronize];
    }
}

Don't forget to create an instance for facebook.h in your AppDelegate.

Upvotes: 0

wc373
wc373

Reputation: 45

Try adding these at the end of your fbDidLogout block:

mFacebook.accessToken = nil;
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
    NSString* domainName = [cookie domain];
    NSRange domainRange = [domainName rangeOfString:@"facebook"];
    if(domainRange.length > 0)
    {
        [storage deleteCookie:cookie];
    }
}

Upvotes: 2

Xiaochen Du
Xiaochen Du

Reputation: 176

Are you using FB mobile Login Dialog? In Facebook.m, add the following code to remove cookies at m.facebook.com domain.

- (void)invalidateSession {

   ...
    NSArray* facebookMCookies = [cookies cookiesForURL:
                                [NSURL URLWithString:@"https://m.facebook.com"]];

    for (NSHTTPCookie* cookie in facebookMCookies) {
        [cookies deleteCookie:cookie];
    }

  ...
}

Upvotes: 4

Nilesh Kikani
Nilesh Kikani

Reputation: 2618

Just add this line to fbDidLogout method

[mFacebook invalidateSession];

This will surely make you logout from Facebook.

Upvotes: 0

Related Questions