ios NSHTTPCookieStorage 状态未在应用退出时保存。有没有明确的知识/文档?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5837702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:46:14  来源:igfitidea点击:

NSHTTPCookieStorage state not saved on app exit. Any definitive knowledge/documentation out there?

objective-cioscookiesnshttpcookie

提问by kball

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

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

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.

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

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 后很快退出,则在重新启动时,那些 sessionOnly=FALSE cookie 将恢复到以前的状态,并且最近的更新丢失。

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

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

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.

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

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

iPhone: NSHTTPCookie is not saved across app restarts

iPhone:NSHTTPCookie 不会在应用程序重新启动时保存

NSHTTPCookies refuse to be deleted

NSHTTPCookies 拒绝删除

deleted NSHTTPCookie returns if app is terminated

如果应用程序终止,则删除 NSHTTPCookie 返回

采纳答案by Nariman

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

我认为答案在于您的问题中链接到的 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.

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

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

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

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

回答by Kuldeep Singh

I also got the same problem but i found a solution. I saved the cookies as it get created by the browser and then recreate them as app restarts.

我也遇到了同样的问题,但我找到了解决方案。我保存了浏览器创建的 cookie,然后在应用程序重新启动时重新创建它们。

1) Save cookie when they get created by uiwebview.

1) 在 uiwebview 创建时保存 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) Now recreate them as app restarts:

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

}

thanks

谢谢

回答by Zaky German

Sounds like an underlying NSUserDefaults-style "synchronize" call is required. I think your best shot is managing all your app's cookies separately in the standard NSUserDefaults and synching any missing ones into the NSHTTPCookieStorage's at startup. Or see if there's some private synchronize method if you're feeling brave :)

听起来像是需要底层 NSUserDefaults 样式的“同步”调用。我认为最好的办法是在标准 NSUserDefaults 中单独管理所有应用程序的 cookie,并在启动时将任何丢失的 cookie 同步到 NSHTTPCookieStorage 中。或者,如果你觉得勇敢,看看是否有一些私有的同步方法:)