0
        NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

        NSArray *cookies = [storage cookiesForURL:[NSURL URLWithString:@"http://www.facebook.com/"]];
        NSLog(@"old cookies!: %@",cookies);
        NSHTTPCookie *cookie;
        for (cookie in [storage cookies]) {
            [storage deleteCookie:cookie];
        }
        NSLog(@"new cookies!: %@",cookies);

I am trying to delete the facebook cookie in a logout function but no cookies are deleted. I do not understand why. Any help will be greatly appreciated.

James
  • 2,951
  • 8
  • 41
  • 55
  • whoops solved my own problem: It doesn't delete the cookie, it deletes it from NSHTTPCookieStorage so if you re- call cookies = [storage cookiesForURL:[NSURL URLWithString:@"http://www.facebook.com/"]]; then it will work – James Jul 21 '11 at 16:35
  • Can you elaborate your solution please, as I have the same problem with facebook cookies. When I logout, remove all cookies, and then restart the app it still remembers all of the cookies and so retains the login info. – TheBestBigAl May 18 '12 at 10:00

1 Answers1

0

If you want your changes to the NSHTTPCookieStorage to be retained, you'll also want to synchronize standardUserDefaults after modifying the cookie store:

[[NSUserDefaults standardUserDefaults] synchronize];

To prevent this from slowing down your app, you may also want to call this on a background thread like so:

dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);

dispatch_async(backgroundQueue, ^{
    //TODO: Cookie deletion logic here
});

EDIT:

If you just need to disregard cookies altogether for a given NSURLRequest, you can do so with:

[request setHTTPShouldHandleCookies:NO];

Where request is your instance of NSURLRequest.

Brian Bethke
  • 154
  • 6