final class WebCacheCleaner {
class func clear() {
URLCache.shared.removeAllCachedResponses()
HTTPCookieStorage.shared.removeCookies(since: Date.distantPast)
print("[WebCacheCleaner] All cookies deleted")
WKWebsiteDataStore.default().fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in
records.forEach { record in
WKWebsiteDataStore.default().removeData(ofTypes: record.dataTypes, for: [record], completionHandler: {})
print("[WebCacheCleaner] Record \(record) deleted")
}
}
}
}
// Usage
WebCacheCleaner.clear()
较旧版本:
NSURLCache.sharedURLCache().removeAllCachedResponses()
if let cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies {
for cookie in cookies {
NSHTTPCookieStorage.sharedHTTPCookieStorage().deleteCookie(cookie)
}
}
URLCache.shared.removeAllCachedResponses()
URLCache.shared.diskCapacity = 0
URLCache.shared.memoryCapacity = 0
if let cookies = HTTPCookieStorage.shared.cookies {
for cookie in cookies {
HTTPCookieStorage.shared.deleteCookie(cookie)
}
}
6条答案
按热度按时间h7appiyu1#
你可以用
Swift 3.0
您还可以更改
NSURLRequest
该高速缓存策略Swift 3.0
有关缓存策略的详细信息:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/index.html#//apple_ref/c/tdef/NSURLRequestCachePolicy
kokeuurv2#
Swift 4:
较旧版本:
js5cn81o3#
Swift 3.
ivqmmu1c4#
Swift 4
如果想要获得私密的浏览体验,您可能还需要按照以下步骤设置
WKWebViewConfiguration
对象的websiteDataStore
属性。hivapdat5#
真的很管用
ghg1uchk6#
在我看来,这是一个很好的解决办法
使用方法: