前两天我在做uiwebview通过cookies自动登录的时候,发现app退出后cookies丢失,搞了一天找不出原因,最后发现ios的cookies需要程序员手动设置保存时间,默认下,cookies的保存时间是app退出后cookie就会被清掉!
怎么保存呢?简单得让人哭笑不得!在app进入后台的时候指向下面代码(不喜欢在delegate中执行,那就通过接受进入后台的通知):
NSHTTPCookie *cookie;
NSHTTPCookieStorage *nCookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookiesURL = [nCookies cookiesForURL:[NSURL URLWithString: youURL]]; //这个是主页的url,不是登录页的url
NSHTTPCookieStorage *nCookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookiesURL = [nCookies cookiesForURL:[NSURL URLWithString: youURL]]; //这个是主页的url,不是登录页的url
for (id c in cookiesURL)
{
if ([c isKindOfClass:[NSHTTPCookie class]])
{
cookie=(NSHTTPCookie *)c;
if ([cookie.name isEqualToString:@" PHPSESSID"]) { // 我的cookies的名字是 " PHPSESSID",你在上一行打个断点看看你的cookies的name是什么
NSDate *expiresDate = [NSDate dateWithTimeIntervalSinceNow:3600*24*30*12]; //当前点后,保存一年左右
NSArray *cookies = [NSArray arrayWithObjects:cookie.name, cookie.value, expiresDate, cookie.domain, cookie.path, nil];
if(cookies){
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:[cookies objectAtIndex:0] forKey:NSHTTPCookieName];
[cookieProperties setObject:[cookies objectAtIndex:1] forKey:NSHTTPCookieValue];
[cookieProperties setObject:[cookies objectAtIndex:2] forKey:NSHTTPCookieExpires];
[cookieProperties setObject:[cookies objectAtIndex:3] forKey:NSHTTPCookieDomain];
[cookieProperties setObject:[cookies objectAtIndex:4] forKey:NSHTTPCookiePath];
NSHTTPCookie *cookieuser = [NSHTTPCookie cookieWithProperties:cookieProperties];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookieuser];
}
break;
}
}
}
{
if ([c isKindOfClass:[NSHTTPCookie class]])
{
cookie=(NSHTTPCookie *)c;
if ([cookie.name isEqualToString:@" PHPSESSID"]) { // 我的cookies的名字是 " PHPSESSID",你在上一行打个断点看看你的cookies的name是什么
NSDate *expiresDate = [NSDate dateWithTimeIntervalSinceNow:3600*24*30*12]; //当前点后,保存一年左右
NSArray *cookies = [NSArray arrayWithObjects:cookie.name, cookie.value, expiresDate, cookie.domain, cookie.path, nil];
if(cookies){
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:[cookies objectAtIndex:0] forKey:NSHTTPCookieName];
[cookieProperties setObject:[cookies objectAtIndex:1] forKey:NSHTTPCookieValue];
[cookieProperties setObject:[cookies objectAtIndex:2] forKey:NSHTTPCookieExpires];
[cookieProperties setObject:[cookies objectAtIndex:3] forKey:NSHTTPCookieDomain];
[cookieProperties setObject:[cookies objectAtIndex:4] forKey:NSHTTPCookiePath];
NSHTTPCookie *cookieuser = [NSHTTPCookie cookieWithProperties:cookieProperties];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookieuser];
}
break;
}
}
}
也就是说,每次退出都会讲保存时间更新为一年,所以不用担心cookies过期问题!有疑问可以加qq :1246489125,
陆陆续续有人加qq提问,基本上都是这个问题:基本都是在[webView loadRequest:登录url],这是错的,应该load主页的url,没用保存cookies的话会自动转跳到登录url的,如果load的是登录url,那设置了cookie也没用!!!
陆陆续续有人加qq提问,基本上都是这个问题:基本都是在[webView loadRequest:登录url],这是错的,应该load主页的url,没用保存cookies的话会自动转跳到登录url的,如果load的是登录url,那设置了cookie也没用!!!
好了现在试试是不是可以自动更新了!