在UIWebView中保持丢失php会话cookie [英] Keep losing php session cookie in UIWebView

查看:146
本文介绍了在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:mydomain.compath:/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] ]];

[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.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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆