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.

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:@"facebook.com/"]]; then it will workJames
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

1 Answers


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


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.