清除WKWebiew缓存的任何属性或方法

时间:2017-05-03 17:58:27

标签: swift3 wkwebview

我正在使用WKWebView登录内容管理系统。构建应用程序后,WKWebView仍然登录到网站。如何清除存储此信息的缓存?

2 个答案:

答案 0 :(得分:0)

我找到了这个答案here

if #available(iOS 9.0, *)
{
    let websiteDataTypes = NSSet(array: [WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache])
    let date = NSDate(timeIntervalSince1970: 0)

    WKWebsiteDataStore.default().removeData(ofTypes: websiteDataTypes as! Set<String>, modifiedSince: date as Date, completionHandler:{ })
}
else
{
    var libraryPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.libraryDirectory, FileManager.SearchPathDomainMask.userDomainMask, false).first!
    libraryPath += "/Cookies"

    do {
        try FileManager.default.removeItem(atPath: libraryPath)
    } catch {
        print("error")
    }
    URLCache.shared.removeAllCachedResponses()
}

答案 1 :(得分:0)

我在这里找到了以下代码:Removing cached data for WKWebView

let dataStore = WKWebsiteDataStore.default()
    dataStore.fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { (records) in
        for record in records {
            print(record.displayName)
            if record.displayName.contains("facebook") {
                dataStore.removeData(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(), for: [record], completionHandler: {
                    print("Deleted: " + record.displayName);
                })
            }
        }