NSHTTPCookies 拒绝删除
我的应用程序需要一个注销按钮,我有以下代码:(
while ([[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies] count] != 0) {
for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
}
while 只是为了确保它们被删除,但它只运行一次)
如果我在上面的代码输出后 NSLog 共享 cookie 存储的描述数组为空。然而,我终止应用程序或只是关闭它,然后在应用程序启动后第一件事 NSLog 共享 cookie 存储的描述,所有 cookie 仍然存在。
我尝试在 for 循环中将 Cookie 设置为 nil,甚至尝试将 dealloc 发送到 cookie(我知道你不应该这样做,但我现在正在尝试任何事情)
I need a log out button for my app, I have the below code:
while ([[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies] count] != 0) {
for (NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
}
(the while is just there to make sure they get deleted, it only runs once however)
If I NSLog the description of shared cookie storage after the above code it outputs that the array is empty. However I terminate the app or just close it, and then NSLog the description of the shared cookie storage the first thing after the app starts, all the cookies are still there.
I have tried setting Cookie
to nil in the for loop, and even tried sending dealloc to the cookies (I know you shouldn't do that but I'm now trying anything)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我制作了一个示例项目来重现此问题 - 并发现只有当应用程序收到 SIGKILL 信号时才会发生这种情况,例如当调试器在 Xcode 中停止。在我的实验中,未处理的异常、崩溃、exit() 和 abort() 不会导致 NSHTPPCookieStorage 丢失数据。
由于这看起来像是仅调试问题(仅在使用调试器时发生),我关闭了 雷达 我之前填的。
不过,我无法测试所有内容:请随意使用示例项目来查看其他崩溃来源是否会触发 cookie 丢失。
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.
I couldn't test everything though: feel free to use the sample project to see if other source of crashes could trigger a cookies loss.
问题似乎是 cookie 被缓存并没有立即保存到文件中。
如果您添加对
[storage _saveCookies]
的调用,那么它就会起作用 - 即使您随后立即终止应用程序,它们也会永远消失。当然,该方法是私有API,因此它不会在App Store上为您提供帮助。要是能找到一些触发它的方法就好了!我还发现以下 CoreFoundation API 运行良好 - 但不幸的是 Apple 也没有公开它:
...
The problem seems to be that the cookies are cached and not saved out to a file immediately.
If you add a call to
[storage _saveCookies]
then it works - they are gone for good, even if you terminate the app straight afterwards. Of course, that method is private API, so it won't help you on the App Store. It would be good to find some way to trigger it!I also found that the following CoreFoundation API works well - but unfortunately it is not exposed by Apple either:
...
我发现以下代码可以工作。
I have found the following code to work.