在 UIWebView 中不断丢失 php 会话 cookie

Keep losing php session cookie in UIWebView(在 UIWebView 中不断丢失 php 会话 cookie)

本文介绍了在 UIWebView 中不断丢失 php 会话 cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是论坛的新手,对 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 中,并在进入前台/打开应用程序时再次加载,就像这里推荐的那样:如何设置我的 web 视图加载已登录的用户 -iPhone - 尽管如此,我仍然失去登录.p>

谁能给我指个方向?非常感谢!

我目前正在这样做(根据下面的帖子):

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 名称], [cookie 值]];[newRequest setValue:cookieString forHTTPHeaderField:@"Cookie"];NSLog(@"插入 cookie 到请求中:%@", cookie);}[self.webView loadRequest:newRequest];

解决方案

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

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

NSURL *myURL = ....NSMutableRequest *mutableRequest = ....NSArray *cookiesToSet = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:myURL];for (NSHTTPCookie *cookiesToSet 中的cookie) {[cookieStringToSet appendFormat:@"%@=%@;", cookie.name, cookie.value];}如果(cookieStringToSet.length){[mutableRequest setValue:cookieStringToSet forHTTPHeaderField:@"Cookie"];}

它有效

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

解决方案

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 NSUserDefaults or something else to store the cookies.

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

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

这篇关于在 UIWebView 中不断丢失 php 会话 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 UIWebView 中不断丢失 php 会话 cookie

基础教程推荐