The status of NSHTTPCookieStorage is not saved when you exit the application. Any final knowledge / documentation there?

Fighting this problem and inability to implement a custom cookie management system.

It seems that some hidden iOS HTTP implementation level does not allow cookies to be processed correctly without sessions. Each time an HTTP response sets or removes a cookie, immediately checking the NSHTTPCookieStorage cookies gives the expected results and indicates the correct sessionOnly value.

But if the application closes shortly after the cookie updates response, after restarting these cookies, sessionOnly = FALSE will be returned to the previous state and the last lost updates.

Cookies set / deleted by response header or NSHTTPCookieStorage setCookie: does not matter.

Some voodoo caching / synchronization should happen behind the scenes. It takes up to 5 seconds for the cookie to become permanent.

Anyone who has or can point to any specific explanation for this behavior? Is this a mistake, simple and simple? Or some kind of undocumented function whose purpose I cannot understand?

Some code you can use to play:

- (void)applicationDidBecomeActive:(UIApplication *)application { [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; NSHTTPCookie *cookie; for (cookie in [NSHTTPCookieStorage sharedHTTPCookieStorage].cookies) { NSLog(@"%@=%@", cookie.name, cookie.value); } NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary]; [cookieProperties setObject:@"testCookie" forKey:NSHTTPCookieName]; [cookieProperties setObject:[NSString stringWithFormat:@"%f", [[NSDate date] timeIntervalSince1970]] forKey:NSHTTPCookieValue]; [cookieProperties setObject:@"www.example.com" forKey:NSHTTPCookieDomain]; [cookieProperties setObject:@"www.example.com" forKey:NSHTTPCookieOriginURL]; [cookieProperties setObject:@"/" forKey:NSHTTPCookiePath]; [cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion]; // set expiration to one month from now [cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires]; cookie = [NSHTTPCookie cookieWithProperties:cookieProperties]; [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie]; } 

This code should output a new value every time it starts. Instead, you will see that if you exit the application quickly, the value will not change.

Some possible stack overflow problems:

iphone NSHTTPCookieStorage available to reopen application

iPhone: NSHTTPCookie not saved through application reloads

NSHTTPCookies refuse to be deleted

remote NSHTTPCookie returns if application terminated

+51
ios objective-c cookies
Apr 29 2018-11-21T00:
source share
3 answers

I think the answer is in one of the SO posts related to your question:

I made a sample project to reproduce this problem, and found that this only happens when the application receives a SIGKILL signal, for example, when the debugger is stopped from Xcode. In my experiments, unhandled exceptions, crashes, exit (), and abort () do not cause NSHTPPCookieStorage to lose data.

Since this looks like a debugging problem (this only happens when using the debugger), I closed the radar that I filled earlier.

You can verify this by restarting the phone in normal mode and noticing that all changes in NSHTTPCookieStorage are correctly saved and reloaded.

+24
Mar 17 '13 at 20:30
source

I also had a problem, but I found a solution. I saved the cookies as they are created by the browser and then recreate them as application reloads.

1) Save the cookie when they are created using 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 restarting applications:

 - (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 } 

thank

+11
Mar 26 '13 at 9:12
source

It looks like a "synchronous" call to NSUserDefaults is needed. I think your best snapshot manages all your cookie applications separately in standard NSUserDefaults and synchronizes any missing cookies in NSHTTPCookieStorage at startup. Or look if there is some private synchronization method if you feel brave :)

+4
Nov 18 2018-11-11T00:
source



All Articles