2017-03-06 89 views
0

我正在製作使用UIWebView的迷你瀏覽器。UIWebView清除所有Cookie

我想爲用戶提供了一種從所有網站刪除所有cookie - 類似於Chrome應用,您可以清除緩存等

中有什麼UIWebView做到這一點的最好辦法?

這裏是我有一個想法:

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

http://stackoverflow.com/questions/4471629/how-to-delete-all-cookies-of-uiwebview(是不是這只是一個重複?) – matt

+0

排序的,這就是我參考一下。但我想讓他們清除所有內容 - 緩存和Cookie –

+0

@TomHammond旁註,不應使用Apple多次提示的UIWebView,而應使用WKWebView。 – 2017-03-06 18:45:07

回答

3

使用此清理餅乾以及緩存。

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()