ios 如何删除 UIWebView 的所有 cookie?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4471629/
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
How to delete all cookies of UIWebView?
提问by Vaibhav Saran
In my application, I have a UIWebview
that loads linkedin auth page for login. When user logs in, cookies saves into the application.
在我的应用程序中,我有一个UIWebview
加载linkedin 身份验证页面以进行登录。当用户登录时,cookie 会保存到应用程序中。
My app has a logout button that is not related to linkedin login. So when user clicks on this button, he logs off from the app. I want that this log off will clear his linkedin cookies also from the app, so that user will log out completely.
我的应用程序有一个与linkedin 登录无关的注销按钮。因此,当用户单击此按钮时,他会从应用程序中注销。我希望此注销也会从应用程序中清除他的linkedin cookie,以便用户完全注销。
回答by Sergio Moura
According to this question, you can go through each cookie in the "Cookie Jar" and delete them, like so:
根据这个问题,您可以遍历“Cookie Jar”中的每个 cookie 并删除它们,如下所示:
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [storage cookies]) {
[storage deleteCookie:cookie];
}
[[NSUserDefaults standardUserDefaults] synchronize];
回答by Cai
Just wanted to add some info regarding this.
只是想添加一些关于此的信息。
In OS X 10.9/iOS 7and later, you can use -resetWithCompletionHandler:to clear the cookies and cache etc. of the whole app from your sharedSession
:
在OS X 10.9/ iOS 7及更高版本中,您可以使用-resetWithCompletionHandler:清除整个应用程序的 cookie 和缓存等sharedSession
:
Empties all cookies, caches and credential stores, removes disk files, flushes in-progress downloads to disk, and ensures that future requests occur on a new socket.
清空所有 cookie、缓存和凭证存储,删除磁盘文件,将正在进行的下载刷新到磁盘,并确保未来的请求发生在新的套接字上。
[[NSURLSession sharedSession] resetWithCompletionHandler:^{
// Do something once it's done.
}];
The for-In loopwith deleteCookie:
sounds like modifying while enumerating a collectionto me. (Don't know, could be a bad idea?)
该for-in循环与deleteCookie:
喜欢的声音,同时枚举集合修改给我。(不知道,可能是个坏主意?)
回答by Saleh Enam Shohag
If anyone is looking for Swift Solution:
如果有人正在寻找 Swift 解决方案:
let storage = HTTPCookieStorage.shared
if let cookies = storage.cookies{
for cookie in cookies {
storage.deleteCookie(cookie)
}
}
回答by Hyman kallis
You could make a function inside the html of the WebView, that cleans the cookies.
您可以在 WebView 的 html 中创建一个函数来清除 cookie。
If you need the cleaning to be done only once you could trigger this function with a Titanium event, only when the app starts.
如果您只需要清洁一次,您可以使用 Titanium 事件触发此功能,仅在应用程序启动时。