title: Shared instance of NSHTTPCookieStorage does not persist cookies
body: <p>I'm developing an application where the server hands me a cookie to identify the user.</p>
  
  <p>My successive requests need to have that cookie to have the response that the user expects.
  What I can't understand is how and when the shared instance of NSHTTPCookieStorage loses its cookies.</p>
  
  <p>The first solution I used is to archive and save the cookies from my server to user defaults on application terminations then clear existing ones from my server on application launch and reset the ones I stored.
  Through the developing process I didn't encounter problems as the sessions for debugging are very short and didn't usually need to put the app in background.</p>
  
  <p>During beta testing, troubles started.
  The hack I came with is to save the cookies not only on application termination but also after the API calls that hand me back these cookies. And to load the saved cookies not only on app launch but also when the app returns to foreground.</p>
  
  <p>How come the NSHTTPCookieStorage share instance gets rid of these cookies and what's the best practice to deal with it as it's a vital part of my app and I can't trust such a hacked solution if not backed up by more experienced developers.</p>
  
  <p>Thank you in advance for your answers</p>
  
  <p>EDIT:
  Here are the methods to save/read/clear the cookies</p>
  
  <pre><code>-(void)saveStoredCookies
  {
      NSURL *httpUrl = @"http://myServer.com";
      NSURL *httpsUrl = @"https://myServer.com";
  
      NSArray *httpCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:httpUrl];
      NSData *httpCookiesData = [NSKeyedArchiver archivedDataWithRootObject:httpCookies];
      [[NSUserDefaults standardUserDefaults] setObject:httpCookiesData forKey:@"savedHttpCookies"];
  
      NSArray *httpsCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:httpsUrl];
      NSData *httpsCookiesData = [NSKeyedArchiver archivedDataWithRootObject:httpsCookies];
      [[NSUserDefaults standardUserDefaults] setObject:httpsCookiesData forKey:@"savedHttpsCookies"];
  
      [[NSUserDefaults standardUserDefaults] synchronize];
  }
  
  -(void)readStoredCookies
  {
      //clear, read and install stored cookies
      NSURL *httpUrl = @"http://myServer.com";
      NSURL *httpsUrl = @"https://myServer.com";
  
      NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:httpUrl];
      for (NSHTTPCookie *cookie in cookies) {
          [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
      }
      cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:httpsUrl];
      for (NSHTTPCookie *cookie in cookies) {
          [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
      }
  
      NSData *httpCookiesData = [[NSUserDefaults standardUserDefaults] objectForKey:@"savedHttpCookies"];
      if([httpCookiesData length]) {
          NSArray *savedCookies = [NSKeyedUnarchiver unarchiveObjectWithData:httpCookiesData];
          for (NSHTTPCookie *cookie in savedCookies) {
              [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
          }       
      }
      NSData *httpsCookiesData = [[NSUserDefaults standardUserDefaults] objectForKey:@"savedHttpsCookies"];
      if([httpsCookiesData length]) {
          NSArray *savedCookies = [NSKeyedUnarchiver unarchiveObjectWithData:httpsCookiesData];
          for (NSHTTPCookie *cookie in savedCookies) {
              [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
          }       
      }
  }
  
  -(void)clearStoredCookies
  {
      NSURL *httpUrl = @"http://myServer.com";
      NSURL *httpsUrl = @"https://myServer.com";
      NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:httpUrl];
      for (NSHTTPCookie *cookie in cookies) {
          [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
      }
      cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:httpsUrl];
      for (NSHTTPCookie *cookie in cookies) {
          [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
      }
  }
  </code></pre>