本文介绍了NSHTTPCookieStorage 状态未在应用退出时保存.那里有明确的知识/文档吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

苦苦挣扎于这个问题,不愿实施自定义 cookie 管理系统.

Struggling with this problem and loath to implement a custom cookie management system.

iOS 的某些隐藏级别的 HTTP 实现似乎无法正确管理无会话 cookie.任何时候 HTTP 响应设置或删除 cookie,立即检查 NSHTTPCookieStorage cookie 将产生预期结果并指示正确的 sessionOnly 值.

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.

但如果应用在响应更新 cookie 后很快退出,则在重新启动时,那些 sessionOnly=FALSE cookie 将恢复到以前的状态,并且最近的更新丢失.

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.

cookie 是通过响应头设置/删除还是通过 NSHTTPCookieStorage setCookie: 没有区别.

Whether the cookies are set/deleted by a response header or NSHTTPCookieStorage setCookie: makes no difference.

某些缓存/同步伏都教必须在幕后进行.cookie 变得持久所需的时间最长可达 5 秒.

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 在应用程序重新打开时可用吗?

iPhone:NSHTTPCookie 不会在应用重启后保存

NSHTTPCookies 拒绝删除

删除的 NSHTTPCookie 在应用终止时返回

推荐答案

我认为答案在于其中一个SO帖子链接到您的问题:

I think the answer lies in one of the SO posts linked to in your question:

我制作了一个示例项目来重现这个问题——并发现它只会在应用程序收到 SIGKILL 信号时发生,例如调试器从 Xcode 中停止.在我的实验中,未处理异常、崩溃、exit() 和 abort() 不会导致NSHTPPCookieStorage 来丢失数据.

因为这看起来像是一个仅限调试的问题(它只发生在使用调试器),我关闭了我之前填充的雷达.

As this looks like a debugging-only issue (it only occurs when using the debugger), I closed the radar I filled previously.

您可以通过正常重启手机并观察对 NSHTTPCookieStorage 的所有更改是否正确持久化并重新加载来进行测试.

You can test this by restarting the phone normally and observing that all changes to NSHTTPCookieStorage are correctly persisted and reloaded.

这篇关于NSHTTPCookieStorage 状态未在应用退出时保存.那里有明确的知识/文档吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 09:29