title: NSHTTPCookieStorage state not saved on app exit. Any definitive knowledge/documentation out there?
body: <p>Struggling with this problem and loath to implement a custom cookie management system.</p>
  
  <p>It appears some hidden level of iOS's implementation of HTTP fails to manage sessionless cookies properly. Any time an HTTP response sets or deletes a cookie, immediate inspection of NSHTTPCookieStorage cookies will yield the expected results and indicate the correct sessionOnly value.</p>
  
  <p>But if the app quits soon after a response updates cookies, upon relaunch those sessionOnly=FALSE cookies will be reverted to some previous state and the most recent updates lost.</p>
  
  <p>Whether the cookies are set/deleted by a response header or NSHTTPCookieStorage setCookie: makes no difference.</p>
  
  <p>Some caching/syncing voodoo must be going on behind the scenes. The time it takes for the cookie to become persistent can be up to 5 seconds.</p>
  
  <p>ANYONE out there who has or can point to some definitive explanation of this behavior? Is it a bug, plain and simple? Or some undocumented feature whose purpose I can't comprehend?</p>
  
  <p>Some code you can use to reproduce:</p>
  
  <pre><code>- (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];
  
  }
  </code></pre>
  
  <p>This code should output a new value on every launch. Instead you will see that if you quit the app quickly the value is unchanged.</p>
  
  <p>Some possibly related stack overflow questions:</p>
  
  <p><a href="https://stackoverflow.com/questions/4623811/iphone-nshttpcookiestorage-avaible-on-app-reopen">iphone NSHTTPCookieStorage avaible on app reopen?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/2662530/iphone-nshttpcookie-is-not-saved-across-app-restarts">iPhone: NSHTTPCookie is not saved across app restarts</a></p>
  
  <p><a href="https://stackoverflow.com/questions/5747012/nshttpcookies-refuse-to-be-deleted">NSHTTPCookies refuse to be deleted</a></p>
  
  <p><a href="https://stackoverflow.com/questions/5070446/deleted-nshttpcookie-returns-if-app-is-terminated">deleted NSHTTPCookie returns if app is terminated</a></p>