Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Login Logout issue with facebook iOS sdk

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

like image 364
user867662 Avatar asked Feb 02 '12 10:02

user867662


4 Answers

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];
}
like image 91
Ellen S Avatar answered Oct 12 '22 15:10

Ellen S


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 .

like image 29
Yash Soni Avatar answered Oct 12 '22 15:10

Yash Soni


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!

like image 39
Jamal Zafar Avatar answered Oct 12 '22 15:10

Jamal Zafar


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!!!

like image 32
Karim Avatar answered Oct 12 '22 14:10

Karim