NSHTTPCookieStorage state not saved on app exit. Any definitive knowledge/documentation out there? NSHTTPCookieStorage state not saved on app exit. Any definitive knowledge/documentation out there? ios ios

NSHTTPCookieStorage state not saved on app exit. Any definitive knowledge/documentation out there?


I think the answer lies in one of the SO posts linked to in your question:

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.

You can test this by restarting the phone normally and observing that all changes to NSHTTPCookieStorage are correctly persisted and reloaded.


I also got the same problem but i found a solution. I saved the cookies as it get created by the browser and then recreate them as app restarts.

1) Save cookie when they get created by uiwebview.

 NSMutableArray *cookieArray = [[NSMutableArray alloc] init];    for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {        [cookieArray addObject:cookie.name];        NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];        [cookieProperties setObject:cookie.name forKey:NSHTTPCookieName];        [cookieProperties setObject:cookie.value forKey:NSHTTPCookieValue];        [cookieProperties setObject:cookie.domain forKey:NSHTTPCookieDomain];        [cookieProperties setObject:cookie.path forKey:NSHTTPCookiePath];        [cookieProperties setObject:[NSNumber numberWithInt:cookie.version] forKey:NSHTTPCookieVersion];        [cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];        [[NSUserDefaults standardUserDefaults] setValue:cookieProperties forKey:cookie.name];        [[NSUserDefaults standardUserDefaults] synchronize];    }    [[NSUserDefaults standardUserDefaults] setValue:cookieArray forKey:@"cookieArray"];    [[NSUserDefaults standardUserDefaults] synchronize];

2) Now recreate them as app restarts:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    NSMutableArray* cookieDictionary = [[NSUserDefaults standardUserDefaults] valueForKey:@"cookieArray"];     NSLog(@"cookie dictionary found is %@",cookieDictionary);    for (int i=0; i < cookieDictionary.count; i++) {        NSLog(@"cookie found is %@",[cookieDictionary objectAtIndex:i]);        NSMutableDictionary* cookieDictionary1 = [[NSUserDefaults standardUserDefaults] valueForKey:[cookieDictionary objectAtIndex:i]];        NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieDictionary1];        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie]; }    // other code}

thanks


Sounds like an underlying NSUserDefaults-style "synchronize" call is required. I think your best shot is managing all your app's cookies separately in the standard NSUserDefaults and synching any missing ones into the NSHTTPCookieStorage's at startup. Or see if there's some private synchronize method if you're feeling brave :)