【发布时间】:2013-02-06 12:26:31
【问题描述】:
我是论坛的新手,对 iOS 开发也很陌生。我在使用 UIWebView 时遇到问题,我一直失去对设置 phpsession cookie 的 HTML 表单的登录权限(过期设置为 8h,适用于桌面)。似乎 UIWebView 在大约一个小时左右而不是 8 小时后将 cookie 扔掉。我读过 NSHTTPCookieStorage 应该自动处理 cookie,即使在应用程序进入后台模式或退出后也是如此。
NSHTTPCookie 看起来像这样
NSHTTPCookie 版本:0 名称:"PHPSESSID" 值:"6f267fdc94c1ce5fcsdgg49b59a8f46b" expiresDate:2013-02-21 01:27:58 +0000 创建时间:2001-01-01 00:00:01 +0000 (1 ) sessionOnly:FALSE domain:"mydomain.com" path:"/" isSecure:FALSE
我确实在退出/睡眠时将其保存到 NSUserDefaults 中,并在进入前台/打开应用程序时再次加载,就像这里推荐的那样:How to set my web view loaded with already login user -iPhone - 尽管如此,我仍然失去了登录。
谁能给我指个方向?非常感谢!
我目前正在这样做(根据下面的帖子):
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];
【问题讨论】:
标签: ios objective-c xcode cookies uiwebview