|
@@ -336,3 +336,27 @@ extension WebView: WKUIDelegate{
|
|
|
target?.present(alert, animated: true, completion: nil)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+func removeWKWebViewCookies() {
|
|
|
+
|
|
|
+ //iOS9.0以上使用的方法
|
|
|
+ if #available(iOS 9.0, *) {
|
|
|
+ let dataStore = WKWebsiteDataStore.default()
|
|
|
+ dataStore.fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes(), completionHandler: { (records) in
|
|
|
+ for record in records{
|
|
|
+ //清除本站的cookie
|
|
|
+// if record.displayName.contains("sina.com"){//这个判断注释掉的话是清理所有的cookie
|
|
|
+ WKWebsiteDataStore.default().removeData(ofTypes: record.dataTypes, for: [record], completionHandler: {
|
|
|
+ //清除成功
|
|
|
+// NXLLog("清除成功\(record)")
|
|
|
+ })
|
|
|
+// }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ //ios8.0以上使用的方法
|
|
|
+ let libraryPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.libraryDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first
|
|
|
+ let cookiesPath = libraryPath! + "/Cookies"
|
|
|
+ try!FileManager.default.removeItem(atPath: cookiesPath)
|
|
|
+ }
|
|
|
+}
|