title: Keep losing php session cookie in UIWebView
body: <p>I'm new to the forum and also quite new to iOS Dev. I'm having trouble with a UIWebView, where I keep losing login permission to a HTML form which sets a phpsession cookie (with expiration set to 8h, which works on desktop). It seems the UIWebView throws the cookie away after about an hour or so, instead of 8h. I've read the NSHTTPCookieStorage should take care automatically for cookies, even after app enters background mode or quits.</p>
  
  <p>The NSHTTPCookie looks like this</p>
  
  <p><strong>NSHTTPCookie version:0 name:"PHPSESSID" value:"6f267fdc94c1ce5fcsdgg49b59a8f46b" expiresDate:2013-02-21 01:27:58 +0000 created:2001-01-01 00:00:01 +0000 (1) sessionOnly:FALSE domain:"mydomain.com" path:"/" isSecure:FALSE</strong></p>
  
  <p>And I do save it on exit/sleep into NSUserDefaults and load it again when coming to foreground/opening app, like recommended here: <a href="https://stackoverflow.com/questions/8986963/how-to-set-my-web-view-loaded-with-already-login-user-iphone">How to set my web view loaded with already login user -iPhone</a> - Still, I keep losing the login.</p>
  
  <p>Can anyone point me in a direction? Thanks a lot!</p>
  
  <p>I am currently doing this (according to the post underneath):</p>
  
  <pre><code>NSURL *lastURL = [[self.webView request] mainDocumentURL];
  
  if (lastURL.absoluteString == NULL) {
      lastURL = [NSURL URLWithString:@"http://mydomain.com/"];
  }
  
  NSArray *cookiesForDomain = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"http://mydomain.com"]];
  NSMutableURLRequest *newRequest = [NSMutableURLRequest requestWithURL:lastURL];
  
  for (NSHTTPCookie *cookie in cookiesForDomain) {
  
      NSString *cookieString = [NSString stringWithFormat:@"%@=%@", [cookie name], [cookie value]];
  
      [newRequest setValue:cookieString forHTTPHeaderField:@"Cookie"];
  
      NSLog(@"inserted cookie into request: %@", cookie);
  
  }
  
  [self.webView loadRequest:newRequest];
  </code></pre>