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.
The NSHTTPCookie looks like this
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
And I do save it on exit/sleep into NSUserDefaults and load it again when coming to foreground/opening app, like recommended here: How to set my web view loaded with already login user -iPhone - Still, I keep losing the login.
Can anyone point me in a direction? Thanks a lot!
I am currently doing this (according to the post underneath):
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];
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…