苦苦于解决这个问题并且不愿实施自定义cookie管理系统.
看来iOS的一些隐藏级别的HTTP实现无法正确管理无会话cookie.每当HTTP响应设置或删除cookie时,立即检查NSHTTPCookieStorage cookie将产生预期结果并指示正确的sessionOnly值.
但是,如果应用程序在响应更新cookie后立即退出,则重新启动时,这些sessionOnly = FALSE cookie将恢复为某个先前状态,并且最新更新将丢失.
是否通过响应头或NSHTTPCookieStorage setCookie设置/删除cookie:没有区别.
一些缓存/同步伏都教必须在幕后进行.cookie变为持久化所需的时间最长可达5秒.
在那里有谁或者可以指出这种行为的明确解释?这是一个简单明了的bug吗?还是一些我无法理解的无证特征?
您可以使用一些代码重现:
- (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]; }
此代码应在每次启动时输出新值.相反,您会看到如果您快速退出应用程序,则值将保持不变.
一些可能相关的堆栈溢出问题:
iphone NSHTTPCookieStorage可以重新打开应用程序吗?
iPhone:NSHTTPCookie未在应用重启期间保存
NSHTTPCookies拒绝被删除
如果应用程序终止,则删除NSHTTPCookie
我认为答案在于您的问题中链接的SO帖子之一:
我做了一个示例项目来重现这个问题 - 并且发现它只会在应用程序收到SIGKILL信号时发生,就像调试器在Xcode中停止时一样.在我的实验中,未处理的异常,崩溃,exit()和abort()不会导致NSHTPPCookieStorage丢失数据.
由于这看起来只是一个调试问题(只有在使用调试器时才会发生),我关闭了之前填充的雷达.
您可以通过正常重新启动手机并观察NSHTTPCookieStorage的所有更改是否已正确保留并重新加载来对此进行测试.
我也遇到了同样的问题,但我找到了解决方案.我保存了由浏览器创建的cookie,然后在应用程序重新启动时重新创建它们.
1)当uiwebview创建cookie时保存cookie.
NSMutableArray *cookieArray = [[NSMutableArray alloc] init]; for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) { [cookieArray addObject:cookie.name]; NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary]; [cookieProperties setObject:cookie.name forKey:NSHTTPCookieName]; [cookieProperties setObject:cookie.value forKey:NSHTTPCookieValue]; [cookieProperties setObject:cookie.domain forKey:NSHTTPCookieDomain]; [cookieProperties setObject:cookie.path forKey:NSHTTPCookiePath]; [cookieProperties setObject:[NSNumber numberWithInt:cookie.version] forKey:NSHTTPCookieVersion]; [cookieProperties setObject:[[NSDate date] dateByAddingTimeInterval:2629743] forKey:NSHTTPCookieExpires]; [[NSUserDefaults standardUserDefaults] setValue:cookieProperties forKey:cookie.name]; [[NSUserDefaults standardUserDefaults] synchronize]; } [[NSUserDefaults standardUserDefaults] setValue:cookieArray forKey:@"cookieArray"]; [[NSUserDefaults standardUserDefaults] synchronize];
2)现在重新启动它们作为应用程序重新启动:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSMutableArray* cookieDictionary = [[NSUserDefaults standardUserDefaults] valueForKey:@"cookieArray"]; NSLog(@"cookie dictionary found is %@",cookieDictionary); for (int i=0; i < cookieDictionary.count; i++) { NSLog(@"cookie found is %@",[cookieDictionary objectAtIndex:i]); NSMutableDictionary* cookieDictionary1 = [[NSUserDefaults standardUserDefaults] valueForKey:[cookieDictionary objectAtIndex:i]]; NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieDictionary1]; [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie]; } // other code }
谢谢
听起来像是需要基础NSUserDefaults样式的“同步”调用。我认为最好的办法是在标准NSUserDefaults中分别管理所有应用程序的cookie,并在启动时将所有丢失的cookie同步到NSHTTPCookieStorage的cookie中。或者,如果您感到很勇敢,请查看是否有一些私有的同步方法:)