在 UIWebView 中不断丢失 php 会话 cookie

编程入门 行业动态 更新时间:2024-10-12 12:33:06
本文介绍了在 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" path:"/" isSecure:FALSE

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

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

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

NSURL *lastURL = [[self.webView request] mainDocumentURL];if (lastURL.absoluteString == NULL) {lastURL = [NSURL URLWithString:@"mydomain/"];}NSArray *cookiesForDomain = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"mydomain"]];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" 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:@"mydomain/"]; } NSArray *cookiesForDomain = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:@"mydomain"]]; 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

本文发布于:2023-05-29 03:38:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/335893.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:词库加载错误:Could not find file 'D:\淘小白 高铁采集器win10\Configuration\Dict_Sto

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!