Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Facebook Connect logout not deleting login details?

I have used Facebook Connect on another project with relatively few problems, however on my current project it seems that when I call [facebook logout]; it doesn't remove the users details. If I then restart the app, I have the following in the didFinishLaunchingWithOptions function:

facebook = [[Facebook alloc] initWithAppId:@"XXXXXXXXXXXXX" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if([defaults objectForKey:@"FBAccessTokenKey"]
   && [defaults objectForKey:@"FBExpirationDateKey"]){
    facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
    facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];

}

    NSLog(@"startup login");
    [self loginToFacebook];//attempt to login automatically on startup

My loginToFacebook function is this:

- (void)loginToFacebook
{
    NSLog(@"Logging into facebook");
    //set up facebook and login in automatically if possible
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if([defaults objectForKey:@"FBAccessTokenKey"]
       && [defaults objectForKey:@"FBExpirationDateKey"]){
        facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];

    }

    if (![facebook isSessionValid]){

        //get permissions that user will need to agree to us using
        NSArray *permissions = [[NSArray alloc] initWithObjects:
                                @"user_likes",@"read_friendlists",@"offline_access", @"publish_stream",
                            nil];
        //authorise our login
        [facebook authorize:permissions];
        [permissions release];
    }
    else
    {
        [facebook authorize:nil];
    }
}

As far as I am concerned, this time I should be prompted to login with my email and password. I am mostly using the simulator so the Facebook app itself is not the cause, and in the Facebook.h class I have changed

[self authorizeWithFBAppAuth:NO safariAuth:YES];

to

[self authorizeWithFBAppAuth:NO safariAuth:NO];

However what happens is that the facebook window briefly flashes up and disappears, as I would expect it to when I am already logged in. It then loads my id, name, friends list etc from my previous login details. It's no longer possible for me to change users! I should add that I have the following code which prints accordingly on 'logout':

- (void)fbDidLogout{
    NSLog(@"Logged out of facebook");
}

...which prints to the console when [facebook logout] is called. I also have the correct url schemes in place so there is no problem there.

As I say I've got this working on another app, but I can't see what I could've overlooked this time. I welcome any suggestion, as I suspect it's something ridiculously simple.

Edit: I've just tested it out on a device, and the attempt to login to Facebook causes the app to crash. I suspect it's because it's attempting to login using 'stored' info, which doesn't exist because I haven't logged in on the device yet. I'm still investigating but again, if any can see an obvious flaw I'd be very grateful.

Edit2:I've tried removing cookies using safecase's example. I also print out all of the cookies during a) didFinishLaunchingWithOptions and b) during fbDidLogout, and I get the following:

a) `2012-05-18 10:40:40.665 MyApp[15545:17003] ( "",

"<NSHTTPCookie version:0 name:\"c_user\" value:\"634361620\" expiresDate:2012-06-17 09:34:33 +0000 created:2012-05-18 09:34:34 +0000 (3.59026e+08) sessionOnly:FALSE domain:\".facebook.com\" path:\"/\" isSecure:TRUE>",
"<NSHTTPCookie version:0 name:\"csm\" value:\"2\" expiresDate:2012-06-17 09:34:33 +0000 created:2012-05-18 09:34:34 +0000 (3.59026e+08) sessionOnly:FALSE domain:\".facebook.com\" path:\"/\" isSecure:FALSE>",
"<NSHTTPCookie version:0 name:\"datr\" value:\"gxe2T8ZyBzMGb5w3LS29Q0kJ\" expiresDate:2014-05-18 09:34:33 +0000 created:2012-05-18 09:34:34 +0000 (3.59026e+08) sessionOnly:FALSE domain:\".facebook.com\" path:\"/\" isSecure:FALSE>",
"<NSHTTPCookie version:0 name:\"locale\" value:\"en_US\" expiresDate:2012-05-25 09:36:43 +0000 created:2012-05-18 09:36:44 +0000 (3.59027e+08) sessionOnly:FALSE domain:\".facebook.com\" path:\"/\" isSecure:FALSE>",
"<NSHTTPCookie version:0 name:\"lu\" value:\"RgayA7CMIlsAl-lOD2-Y-O3g\" expiresDate:2014-05-18 09:34:33 +0000 created:2012-05-18 09:34:34 +0000 (3.59026e+08) sessionOnly:FALSE domain:\".facebook.com\" path:\"/\" isSecure:FALSE>",
"<NSHTTPCookie version:0 name:\"m_user\" value:\"0%3A0%3A0%3A0%3Av_1%2Cajax_1%2Cwidth_320%2Cpxr_1%2Cgps_1%3A1337333673%3A2\" expiresDate:2012-08-16 09:34:33 +0000 created:2012-05-18 09:34:34 +0000 (3.59026e+08) sessionOnly:FALSE domain:\".facebook.com\" path:\"/\" isSecure:FALSE>",
"<NSHTTPCookie version:0 name:\"s\" value:\"Aa4WdKU-oOFathmK\" expiresDate:2012-06-17 09:34:33 +0000 created:2012-05-18 09:34:34 +0000 (3.59026e+08) sessionOnly:FALSE domain:\".facebook.com\" path:\"/\" isSecure:TRUE>",
"<NSHTTPCookie version:0 name:\"xs\" value:\"125%3AFFIWXjAXDXUMmw%3A2%3A1337333673\" expiresDate:2012-06-17 09:34:33 +0000 created:2012-05-18 09:34:34 +0000 (3.59026e+08) sessionOnly:FALSE domain:\".facebook.com\" path:\"/\" isSecure:TRUE>"

)`

b) 2012-05-18 10:41:16.530 MyApp[15545:17003] ( )

Since it changes to empty, I would assume they are deleted. But on reopening the app, they are all there again.

Edit3: The only way around this I have found is to delete all cookies as soon as the application opens, however this means the user has to login every time, even if they left themselves logged in the last time they had the app open. It's a temporary fix for the moment, I'm still not sure why it's not working as it should be.

like image 266
TheBestBigAl Avatar asked May 18 '12 08:05

TheBestBigAl


2 Answers

You provide this method when user clicks on logout and remove all keys stored in userdefault for facebook

- (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];
       }
   }
}
like image 105
Paresh Navadiya Avatar answered Sep 30 '22 15:09

Paresh Navadiya


Note: as well as FB app stores login information in NSUserDefaults it also stores login information in NSHTTPCookieStorage using name m_user. You can just remove this record to cause FB ask you login information again.

Hope this information will help you.

like image 34
SVGreg Avatar answered Sep 30 '22 15:09

SVGreg