uiwebview清除所有cookie



我正在努力使用UIWebView

我想为用户提供一种从所有站点中删除所有cookie的方法 - 类似于Chrome应用程序,您可以清除高速缓存等。

UIWebView中完成此操作的最佳方法是什么?

这是我有一个想法:

NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in [storage cookies]) {
   [storage deleteCookie:cookie];
}
[[NSUserDefaults standardUserDefaults] synchronize];

使用它来清除cookie以及缓存。

    URLCache.shared.removeAllCachedResponses()
    URLCache.shared.diskCapacity = 0
    URLCache.shared.memoryCapacity = 0
    webView.stringByEvaluatingJavaScript(from: "localStorage.clear();")
    let cookieJar = HTTPCookieStorage.shared
    for cookie in cookieJar.cookies! {
        cookieJar.deleteCookie(cookie)
    }
    UserDefaults.standard.synchronize()

最新更新