NSHTTPCookieStorage状态未保存在应用退出。任何确定的知识/文档在那里? [英] NSHTTPCookieStorage state not saved on app exit. Any definitive knowledge/documentation out there?
问题描述
出现一些隐藏的iOS执行HTTP的问题,无法正确管理无会话的Cookie。任何时候HTTP响应设置或删除cookie,立即检查NSHTTPCookieStorage cookie将产生预期的结果,并指示正确的sessionOnly值。
但如果应用程序很快退出一个响应更新cookie,在重新启动那些sessionOnly = FALSE后,Cookie将恢复到某个先前状态,并且最近的更新丢失。
Cookie是否由响应头或NSHTTPCookieStorage setCookie:没有区别。
一些缓存/同步巫毒必须在幕后进行。
任何人谁有或可以指向这种行为的一些明确的解释,持续时间可以长达5秒。这是一个bug,简单明了吗?
您可以用来重现的一些代码:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
NSHTTPCookie * cookie;
([NSHTTPCookieStorage sharedHTTPCookieStorage] .cookies中的Cookie){
NSLog(@%@ =%@,cookie.name,cookie.value);
}
NSMutableDictionary * cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:@testCookieforKey:NSHTTPCookieName];
[cookieProperties setObject:[NSString stringWithFormat:@%f,[[NSDate date] timeIntervalSince1970]] forKey:NSHTTPCookieValue];
[cookieProperties setObject:@www.example.comforKey:NSHTTPCookieDomain];
[cookieProperties setObject:@www.example.comforKey:NSHTTPCookieOriginURL];
[cookieProperties setObject:@/forKey:NSHTTPCookiePath];
[cookieProperties setObject:@0forKey:NSHTTPCookieVersion];
//设置过期到一个月后
[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];
cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
此代码应在每次启动时输出一个新值。
一些可能相关的堆栈溢出问题:
iphone NSHTTPCookieStorage可在应用程式上重新开启吗?
iPhone:NSHTTPCookie不会保存应用程式重新启动
我认为答案在您的问题中链接到的一个的SO帖子:
我做了一个示例项目来重现这个问题 - 发现
只会在应用程序收到SIGKILL信号时发生,比如当
调试器在Xcode中停止时。在我的实验中,未处理的
异常,崩溃,exit()和abort()不会导致
NSHTPPCookieStorage松散数据。
看起来像一个只调试的问题(它只发生在使用
调试器),我关闭了以前填写的雷达。
您可以通过正常重新启动电话并观察对NSHTTPCookieStorage的所有更改是否正确保留并重新加载来进行测试。
Struggling with this problem and loath to implement a custom cookie management system.
It appears some hidden level of iOS's implementation of HTTP fails to manage sessionless cookies properly. Any time an HTTP response sets or deletes a cookie, immediate inspection of NSHTTPCookieStorage cookies will yield the expected results and indicate the correct sessionOnly value.
But if the app quits soon after a response updates cookies, upon relaunch those sessionOnly=FALSE cookies will be reverted to some previous state and the most recent updates lost.
Whether the cookies are set/deleted by a response header or NSHTTPCookieStorage setCookie: makes no difference.
Some caching/syncing voodoo must be going on behind the scenes. The time it takes for the cookie to become persistent can be up to 5 seconds.
ANYONE out there who has or can point to some definitive explanation of this behavior? Is it a bug, plain and simple? Or some undocumented feature whose purpose I can't comprehend?
Some code you can use to reproduce:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
NSHTTPCookie *cookie;
for (cookie in [NSHTTPCookieStorage sharedHTTPCookieStorage].cookies) {
NSLog(@"%@=%@", cookie.name, cookie.value);
}
NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
[cookieProperties setObject:@"testCookie" forKey:NSHTTPCookieName];
[cookieProperties setObject:[NSString stringWithFormat:@"%f", [[NSDate date] timeIntervalSince1970]] forKey:NSHTTPCookieValue];
[cookieProperties setObject:@"www.example.com" forKey:NSHTTPCookieDomain];
[cookieProperties setObject:@"www.example.com" forKey:NSHTTPCookieOriginURL];
[cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
[cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];
// set expiration to one month from now
[cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires];
cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
}
This code should output a new value on every launch. Instead you will see that if you quit the app quickly the value is unchanged.
Some possibly related stack overflow questions:
iphone NSHTTPCookieStorage avaible on app reopen?
iPhone: NSHTTPCookie is not saved across app restarts
NSHTTPCookies refuse to be deleted
deleted NSHTTPCookie returns if app is terminated
I think the answer lies in one of the SO posts linked to in your question:
I made a sample project to reproduce this issue — and found that it would only occur when the app receives a SIGKILL signal, like when the debugger is stopped from within Xcode. In my experiments, unhandled exceptions, crashes, exit() and abort() don't cause NSHTPPCookieStorage to loose data.
As this looks like a debugging-only issue (it only occurs when using the debugger), I closed the radar I filled previously.
You can test this by restarting the phone normally and observing that all changes to NSHTTPCookieStorage are correctly persisted and reloaded.
这篇关于NSHTTPCookieStorage状态未保存在应用退出。任何确定的知识/文档在那里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!