WKWebsiteDataStore.remove(..) 不会删除 iOS10 中的网站数据



我一直在尝试在将网站加载到WKWebView之前清除所有网站数据,但它似乎不起作用。这是我一直在尝试解决这个问题的代码:

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let dataStore = WKWebsiteDataStore.default()
let allTypes = WKWebsiteDataStore.allWebsiteDataTypes()
print("Reading cached data ...")
dataStore.fetchDataRecords(ofTypes: allTypes) { records1 in
print("Clearing web data (records1)") // Should show old website data
dataStore.removeData(ofTypes: allTypes, for: records1) {
print("Reading data store after clear ...")
dataStore.fetchDataRecords(ofTypes: allTypes) { records2 in
print("Data store after clear (records2)") // Should be empty, but isn't
print("Reading web data from new configuration ...")
let webConfiguration = WKWebViewConfiguration()
let dataStore2 = webConfiguration.websiteDataStore
dataStore2.fetchDataRecords(ofTypes: allTypes) { records3 in
print("New config web data (records3)") // Should be empty, but isn't
self.webView = WKWebView(frame: .zero, configuration: webConfiguration)
self.loadWebsite()
}
}
}
}
}

如您所见,我正在清除所有数据,然后检查数据存储,期望它是空的。但事实并非如此。所有 cookie 和数据仍然存在,就好像删除不起作用一样。然后,我加载一个全新的配置并检查它,同时发现数据仍然存在。

但是,如果我等待一段时间,或者在显示 Web 视图后删除数据,那么当我再次显示它时,数据就消失了。

几乎感觉就像 Web kit 在实际发生之前排队删除数据并调用它的完成块。

关于如何解决这个问题的任何想法?

这对我有用

let cacheTypes = Set<String>([WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache])
let store = WKWebsiteDataStore.default()
store.fetchDataRecords(ofTypes: cacheTypes) { (record) in
store.removeData(ofTypes: cacheTypes, for: record){ }
}

最新更新