Hitendra
Hitendra

Reputation: 1610

How to logout from facebook

Hi, I have download the latest sdk of Facebook from developer.Facebook.com and i implemented the sdk in my code. Now when i post to Facebook the first time it goes to Safari for login to Facebook and after successful login it posts to Facebook and after that it doesn't ask for login but i want to ask for login again. So i wrote the following logout function but it didn't work for me. Does any one have any idea? please help me.

NSLog(@"Logged out of facebook");
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: 0

Views: 72

Answers (2)

Prabhu Natarajan
Prabhu Natarajan

Reputation: 869

According to FaceBook Developer Documentation we should use the following code

 // get the app delegate so that we can access the session property
AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate];

// this button's job is to flip-flop the session from open to closed
if ([FBSession activeSession].isOpen)
{
    // if a user logs out explicitly, we delete any cached token information, and next
    // time they run the applicaiton they will be presented with log in UX again; most
    // users will simply close the app or switch away, without logging out; this will
    // cause the implicit cached-token login to occur on next launch of the application
    [[FBSession activeSession] closeAndClearTokenInformation];
}

Upvotes: 0

sonxurxo
sonxurxo

Reputation: 5718

To logout from Facebook you just have to do:

[FBSession.activeSession closeAndClearTokenInformation];

EDIT 1: This will close the in-memory session. If the login is made through Safari, it's in Safari where the session is still open, and I'm afraid you've got nothing to do with that.

To confirm that, try to open the Safari app (standalone) and enter facebook.com after login in with your application. The session will be still open.

Upvotes: 2

Related Questions