在UIWebView中保持丢失php会话cookie

编程入门 行业动态 更新时间:2024-10-23 23:23:27
本文介绍了在UIWebView中保持丢失php会话cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是新的论坛,也是新的iOS开发。我有一个UIWebView的麻烦,我不断失去登录权限的HTML表单设置phpsession cookie(到期设置为8h,这在桌面上工作)。看起来UIWebView抛出的cookie大约一个小时左右,而不是8h。我已经阅读NSHTTPCookieStorage应该自动照顾cookie,即使应用程序进入后台模式或退出后。

NSHTTPCookie看起来像这样

NSHTTPCookie version:0 name:PHPSESSIDvalue:6f267fdc94c1ce5fcsdgg49b59a8f46bexpiresDate:2013-02-21 01:27:58 +0000 created:2001-01-01 00:00:01 +0000(1)sessionOnly:FALSE domain:mydomainpath:/isSecure:FALSE

睡眠到NSUserDefaults,并在来到前台/打开应用程序时再次加载它,像这里推荐的:如何设置我的网页视图加载已经登录用户-iPhone - 仍然,我继续失去登录。

<谁能指点我的方向?非常感谢!

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

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] ]]; [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 * cookie in cookiesToSet){ [cookieStringToSet appendFormat:@%@ =%@;,cookie.name,cookie.value]; } if(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-07-15 10:24:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110645.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:UIWebView   php   cookie

发布评论

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

>www.elefans.com

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