6
votes

We have got sruck in the iOS facebook login logout issue. When I login to facebook using my application it will prompt for user permission with 'login' and 'cancel' button. But this screen appears only on the very first time. ie Once we logged in using safari or the app and even if we logged out from facebook , application the screen prompting for user permission displays only an 'ok' button. It doesnt allow to sign in as a different user. Why the screen with 'login' and 'cancel' button not displaying each time the application launches? I tried by deleting cookies and removing NSUserDefaults but no luck.

The problem is after logout, I am unable to login to the facebook as another user. It still shows as the same user.

I am calling the below logout function in sdk

(void)logout:(id<FBSessionDelegate>)delegate {

  self.sessionDelegate = delegate;
 [_accessToken release];
 _accessToken = nil;
 [_expirationDate release];
 _expirationDate = nil;

 NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
 NSArray* facebookCookies = [cookies cookiesForURL:
 [NSURL URLWithString:@"http://login.facebook.com"]];
 for (NSHTTPCookie* cookie in facebookCookies) {
   [cookies deleteCookie:cookie];
 }

if ([self.sessionDelegate respondsToSelector:@selector(fbDidLogout)]) {
  [_sessionDelegate fbDidLogout];
}
}

Also in fbDidLogout delegate function I removed all NSUserDefaults objects

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"]) {
    [defaults removeObjectForKey:@"FBAccessTokenKey"];
    [defaults removeObjectForKey:@"FBExpirationDateKey"];
    [defaults synchronize];
}

regrds Shihab

5
could you write how implement your log out method and which delegates do you use? perhaps i can help you, i have been struggling with facebook as well. - Julian Osorio
Sorry, I cannot attach the screen shoot. The issue I am facing is even after logout from my application, I cannot login as another user. I expect that, after logout, when I am trying to loging to the system again, there should be a link to logout from the facebook user name password window. I have seen same in some iOS Facebook applications. - user867662
Did you find any solution? I am facing the same problem. - Burak
Its not possible to login as another user since facebook is logged in globally into your device and you cant log a person out of facebook from your app - Aatish Molasi

5 Answers

5
votes

FBSession openWithBehavior:completionHandler: can be used..

FBSession *fbSession = [[FBSession alloc] initWithPermissions:[NSArray arrayWithObjects:@"email",@"publish_actions",@"publish_stream", nil]];
 [fbSession openWithBehavior:FBSessionLoginBehaviorForcingWebView completionHandler:^(FBSession *session,FBSessionState state, NSError *error){
    [FBSession setActiveSession:fbSession]; // Retain the Active Session.        
}];

For Logging out, Ans by Ellen S.. worked fine for iOS .

7
votes

You can clear the session as well as clearing the cookies with the following code:

FBSession* session = [FBSession activeSession];
[session closeAndClearTokenInformation];
[session close];
[FBSession setActiveSession:nil];

NSHTTPCookieStorage* cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray* facebookCookies = [cookies cookiesForURL:[NSURL URLWithString:@"https://facebook.com/"]];

for (NSHTTPCookie* cookie in facebookCookies) {
    [cookies deleteCookie:cookie];
}
5
votes

I modified fbDidLogout method and it worked, here is the code:

-(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];
        }
    }
}//End of Method

The method successfully logs out the user. Hope this will help!

2
votes

I just figure it out I got in the settings of my iPhone and got to privacy chose the Facebook tab and turn off where it says Applications that have requested access to you Facebook account will appear here. It works!!!

0
votes

When login to set to loginBehavior, so when you exit, with the other account login, won't appear only authorized, without the login screen login.loginBehavior =FBSDKLoginBehaviorWeb; i use facebook 4.11,it's work