7

I need a log out button for my app, I have the below code:

        while ([[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies] count] != 0) {
            for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
                [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
            }
        }

(the while is just there to make sure they get deleted, it only runs once however)

If I NSLog the description of shared cookie storage after the above code it outputs that the array is empty. However I terminate the app or just close it, and then NSLog the description of the shared cookie storage the first thing after the app starts, all the cookies are still there.

I have tried setting Cookie to nil in the for loop, and even tried sending dealloc to the cookies (I know you shouldn't do that but I'm now trying anything)

Monolo
  • 17,926
  • 16
  • 63
  • 102
Jonathan.
  • 51,850
  • 46
  • 174
  • 275
  • I've just put your code in my current project. First I dumped the cookies with `NSLog(@"%@", [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]);` Then executed your code. Relaunch. Cookies are gone. Strange! – Nick Weaver Apr 21 '11 at 16:37
  • I am encountering this same issue and am researching it now. It is causing a lot of conflicts when my app assumes that certain cookies are gone. – casey Sep 29 '11 at 20:19
  • @casey, please post back with your findings (I'll give extra rep as well) – Jonathan. Sep 29 '11 at 21:35
  • I found this answer http://stackoverflow.com/questions/3126002/iphone-app-cookie-delay and implemented their solution, which is a nice idea. Works fine for me it seems so far. Still playing with it. – casey Oct 10 '11 at 05:15
  • 1
    @casey, but thats the opposite? I need the cookies to be deleted? – Jonathan. Oct 10 '11 at 17:01
  • @Jonathan. Well not really. You make the pref file your persistent cookie store. So while your app is open do any cookies changes you want in the cookie storage like normal, and upon open/close of the app, you load/save the cookie storage to the prefs file. See, your issue is upon opening of the app, old cookies come back. In this solution, upon opening the app, you would delete all cookie storage cookies and load in your pref file cookies, resuming the state you thought you had when you shutdown. Been working for me so far. – casey Nov 22 '11 at 17:38
  • I think Casey has the solution. Sounds like the cookies didn't have time to serialize the changes you've made and thus when loaded again they're still there. Remove all cookies at launch and restore from user prefs sounds like good solution. – iceydee Jan 03 '12 at 14:47

3 Answers3

10

The problem seems to be that the cookies are cached and not saved out to a file immediately.

I made a sample project to reproduce this issue — and found that it would only occur when the app receives a SIGKILL signal, like when the debugger is stopped from within Xcode. In my experiments, unhandled exceptions, crashes, exit() and abort() don't cause NSHTPPCookieStorage to loose data.

As this looks like a debugging-only issue (it only occurs when using the debugger), I closed the radar I filled previously.

I couldn't test everything though: feel free to use the sample project to see if other source of crashes could trigger a cookies loss.

Kemenaran
  • 1,391
  • 10
  • 9
  • 2
    Oh thank you so much @Kemenaran, I was totaly lost. To test cookies, do not kill the app from the Xcode and kill the app from the phone before to launch this app from Xcode. – Damien Romito Feb 06 '14 at 06:07
  • Thanks, thats it.For OSX: Instead of stopping the application using Xcode just quit it (right click on the app icon). This makes sure the cookies gets saved – sust86 Jun 24 '15 at 13:07
3

The problem seems to be that the cookies are cached and not saved out to a file immediately.

If you add a call to [storage _saveCookies] then it works - they are gone for good, even if you terminate the app straight afterwards. Of course, that method is private API, so it won't help you on the App Store. It would be good to find some way to trigger it!

I also found that the following CoreFoundation API works well - but unfortunately it is not exposed by Apple either:

extern CFTypeRef _CFHTTPCookieStorageGetDefault();
extern void CFHTTPCookieStorageDeleteAllCookies( CFTypeRef storage );
extern void CFHTTPCookieStorageSyncStorageNow( CFTypeRef storage );

...

CFTypeRef storage = _CFHTTPCookieStorageGetDefault();
CFHTTPCookieStorageDeleteAllCookies( storage );
CFHTTPCookieStorageSyncStorageNow( storage );
FyKnight
  • 81
  • 3
  • I filled rdar://13293418 ([openradar](http://openradar.appspot.com/radar?id=2776403)) to ask for this API to be made public; you can fill a duplicate to give it some weight. – Kemenaran Feb 26 '13 at 10:06
1

I have found the following code to work.

NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookieJar = [storage cookies];

for (NSHTTPCookie *cookie in cookieJar)
{
    [storage deleteCookie:cookie];
}
roff
  • 502
  • 3
  • 5
  • The suggestion that this simply works appears on many SO posts but is quite misleading. @Kemenaran's explanation on exactly when this works and when it doesn't is crucial to understanding this issue and how to test for all scenarios. – Nariman Mar 17 '13 at 20:26