删除 cookie UIWebView
如何删除UIWebView中的cookie?此代码:
NSArray* cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
for (NSHTTPCookie *cookie in cookies) {
[cookies deleteCookie:cookie];
}
删除 cookie,但是当我重新启动应用程序时,NSHTTPCookieStorage 中有相同的 cookie。有时这段代码可以工作,但我想让它每次都能工作。如何解决这个问题?
How to delete cookies in UIWebView? This code:
NSArray* cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
for (NSHTTPCookie *cookie in cookies) {
[cookies deleteCookie:cookie];
}
deletes cookies but when I restart my application, there are same cookies in NSHTTPCookieStorage. Sometimes this code works, but i want to make it work everytime.How to solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试这样的事情:
Try something like this:
这对我有用:
This worked for me:
由于某些奇怪的原因,删除单个 cookie 并不总是有效。要真正删除 cookie,您需要存储非特定 cookie,然后重新加载它们,然后迭代所有 cookie 并删除它们,如下所示
deleting a single cookie does not always work for some odd reason. To truly get a cookie deleted you will need to store the not specific cookie and then reload them and then iterate through all cookies and delete them such as this
与前一个类似但更简单:
“最佳答案”很糟糕,因为它允许删除指定具体 URL 的 cookie。例如,您删除了“login.facebook.com”的 cookie,但您可能会错过“www.login.facebook.com”
The similar for previous one but simplier:
The "best answer" is bad because it allows to delete cookies for the specified concrete URLs. So for example you delete cookie for "login.facebook.com" but you may miss "www.login.facebook.com"
确保最后调用:
[[NSUserDefaults standardUserDefaults]同步];
...就像一个魅力...
Make sure to call:
[[NSUserDefaults standardUserDefaults] synchronize];
in the end... Works like a charm...