gpt4 book ai didi

objective-c - NSHTTPCookieStorage 状态未在应用退出时保存。那里有任何明确的知识/文档吗?

转载 作者:IT老高 更新时间:2023-10-28 11:32:20 24 4
gpt4 key购买 nike

正在努力解决这个问题并且不愿实现自定义 cookie 管理系统。

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

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

cookie 是由响应头设置/删除还是由 NSHTTPCookieStorage setCookie: 设置/删除没有区别。

必须在幕后进行一些缓存/同步 voodoo。 cookie 持久化所需的时间最长可达 5 秒。

任何人有或可以指出对这种行为的一些明确解释?它是一个简单的错误吗?还是一些我无法理解其用途的未记录功能?

一些你可以用来重现的代码:

- (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 avaible on app reopen?

iPhone: NSHTTPCookie is not saved across app restarts

NSHTTPCookies refuse to be deleted

deleted NSHTTPCookie returns if app is terminated

最佳答案

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

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.

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

关于objective-c - NSHTTPCookieStorage 状态未在应用退出时保存。那里有任何明确的知识/文档吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5837702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com