user4951
user4951

Reputation: 33140

How to Log Out of Facebook in iOS

I know how to log in

[FBSession openActiveSessionWithReadPermissions:readPermissions allowLoginUI:YES completionHandler:
 ^(FBSession *session,FBSessionState state, NSError *error) {
     [self sessionStateChanged:session state:state error:error];//This is done on main thread already
 }];

The thing is what to do at logout, say when user press the logout button?

Currently I simply do

[FBSession.activeSession closeAndClearTokenInformation];
[[NSNotificationCenter defaultCenter] postNotificationName:FACEBOOKSESSIONCHANGED object:self];

However, I do not think that's true. [FBSession.activeSession closeAndClearTokenInformation]; should be called at [self sessionStateChanged:session state:state error:error]; AFTER logging out.

So what should I do to logout?

I check scrumptuous sample from facebook, I couldn't find their mechanism to logout. It seems that they use specialized facebook viewController that has a delegate. But none of the delegate is called.

Upvotes: 2

Views: 1468

Answers (2)

Jerry Dubuke
Jerry Dubuke

Reputation: 186

not sure if anyone is still getting bitten by this, but with the latest version of the Facebook SDK, i cannot get an iPad to be forced to log in after logging out. There is still some sort of weirdness here - the 'clear cookies' code is being jumped over - is there a different way to clear cookies in the iPad? Jerry

Upvotes: 0

Pushpak Narasimhan
Pushpak Narasimhan

Reputation: 454

Logging out mechanism , they just invalidate the access token by releasing it and setting it to nil. But if your login is using safari then still the cookies would exists. So next time if user open safari and try to access Facebook it would say that it is already authorized. To delete the cookies I use the following method

- (void)fbDidLogout
{
  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];
       }
   }
}

To be more sure you can also call as some bugs are there which return open session even after calling closeAndClearTokenInformation. https://developers.facebook.com/bugs/497294866962479?browse=search_507cb8ebc4f025673237228

[FBSession.activeSession closeAndClearTokenInformation];
[FBSession.activeSession close];
[FBSession setActiveSession:nil];

Other option is to use the following condition while checking for active session .

if (FBSession.activeSession.isOpen)
 //logged in
else
 //not logged in

Because FBSession.activeSession.state was returning FBSessionStateOpen, not FBSessionStateCreatedTokenLoaded.

Upvotes: 1

Related Questions