xcode 在 UIWebView 中不断丢失 php 会话 cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14997879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:49:26  来源:igfitidea点击:

Keep losing php session cookie in UIWebView

iosobjective-cxcodecookiesuiwebview

提问by riccardolardi

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.

我是论坛的新手,也是 iOS 开发的新手。我在使用 UIWebView 时遇到问题,我一直失去对设置 phpsession cookie 的 HTML 表单的登录权限(过期时间设置为 8h,可在桌面上运行)。似乎 UIWebView 在大约一个小时左右后扔掉了 cookie,而不是 8 小时。我读过 NSHTTPCookieStorage 应该自动处理 cookie,即使在应用程序进入后台模式或退出之后。

The NSHTTPCookie looks like this

NSHTTPCookie 看起来像这样

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

NSHTTPCookie 版本:0 名称:“PHPSESSID” 值:“6f267fdc94c1ce5fcsdgg49b59a8f46b” expiresDate:2013-02-21 01:27:58 +0000 created:2001-01-01 00:000:01 +0SEAL00:01 域(域) “mydomain.com”路径:“/” 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.

我确实在退出/睡眠时将它保存到 NSUserDefaults 中,并在进入前台/打开应用程序时再次加载它,就像这里推荐的那样:如何设置我的 web 视图加载了已经登录的用户 -iPhone- 尽管如此,我仍然无法登录。

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];

回答by Mert

I use in my app many requests and every time when I send a request I get the cookies for the url from NSHTTPCookieStorage. I do not use NSUserDefaultsor something else to store the cookies.

我在我的应用程序中使用了许多请求,每次发送请求时,我都会从NSHTTPCookieStorage. 我不使用NSUserDefaults或其他东西来存储 cookie。

When I send a new request I set the needed cookies my self

当我发送新请求时,我自己设置了所需的 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"];
}

And it works

它有效