继续在UIWebView中丢失php会话cookie

ric*_*rdi 3 cookies xcode objective-c uiwebview ios

我是论坛的新手,也是iOS Dev的新手.我遇到了UIWebView的问题,我不断失去对HTML表单的登录权限,该表单设置了一个phpsession cookie(过期设置为8h,适用于桌面).似乎UIWebView在大约一个小时左右之后抛弃了cookie,而不是8h.我已经阅读过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域: "mydomain.com"路径:"/"isSecure:FALSE

并且我确实将它保存在退出/睡眠中进入NSUserDefaults并在进入前台/打开应用程序时再次加载它,如下所示:如何设置我的网页视图已加载已登录用户-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];
Run Code Online (Sandbox Code Playgroud)

Mer*_*ert 5

我在我的应用程序中使用了很多请求,每当我发送请求时,我都会从网址获取cookie NSHTTPCookieStorage.我不使用NSUserDefaults或其他东西来存储cookie.

当我发送新请求时,我自己设置所需的cookie

NSURL *myURL = ....
NSMutableRequest *mutableRequest = ....
NSArray *cookiesToSet = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:myURL];
for (NSHTTPCookie *cookie in cookiesToSet) {
    [cookieStringToSet appendFormat:@"%@=%@;", cookie.name, cookie.value];
}

if (cookieStringToSet.length) {
    [mutableRequest setValue:cookieStringToSet forHTTPHeaderField:@"Cookie"];
}
Run Code Online (Sandbox Code Playgroud)

它有效