How to remove cache in WKWebview?
66,985
Solution 1
Updated Swift 5 version:
WKWebsiteDataStore.default().removeData(ofTypes: [WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache], modifiedSince: Date(timeIntervalSince1970: 0), completionHandler:{ })
Swift version:
if #available(iOS 9.0, *) {
let websiteDataTypes = NSSet(array: [WKWebsiteDataTypeDiskCache, WKWebsiteDataTypeMemoryCache])
let date = NSDate(timeIntervalSince1970: 0)
WKWebsiteDataStore.defaultDataStore().removeDataOfTypes(websiteDataTypes as! Set<String>, modifiedSince: date, completionHandler:{ })
} else {
var libraryPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomainMask.UserDomainMask, false).first!
libraryPath += "/Cookies"
do {
try NSFileManager.defaultManager().removeItemAtPath(libraryPath)
} catch {
print("error")
}
NSURLCache.sharedURLCache().removeAllCachedResponses()
}
Swift 3 version:
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()
}
Solution 2
In iOS 9
// Optional data
NSSet *websiteDataTypes
= [NSSet setWithArray:@[
WKWebsiteDataTypeDiskCache,
//WKWebsiteDataTypeOfflineWebApplicationCache,
WKWebsiteDataTypeMemoryCache,
//WKWebsiteDataTypeLocalStorage,
//WKWebsiteDataTypeCookies,
//WKWebsiteDataTypeSessionStorage,
//WKWebsiteDataTypeIndexedDBDatabases,
//WKWebsiteDataTypeWebSQLDatabases,
//WKWebsiteDataTypeFetchCache, //(iOS 11.3, *)
//WKWebsiteDataTypeServiceWorkerRegistrations, //(iOS 11.3, *)
]];
// All kinds of data
// NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
// Date from
NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
// Execute
[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
// Done
}];
Solution 3
Remove Cache and Cookies
# WKWebView+Clean.swift
import WebKit
extension WKWebView {
class func clean() {
guard #available(iOS 9.0, *) else {return}
HTTPCookieStorage.shared.removeCookies(since: Date.distantPast)
WKWebsiteDataStore.default().fetchDataRecords(ofTypes: WKWebsiteDataStore.allWebsiteDataTypes()) { records in
records.forEach { record in
WKWebsiteDataStore.default().removeData(ofTypes: record.dataTypes, for: [record], completionHandler: {})
#if DEBUG
print("WKWebsiteDataStore record deleted:", record)
#endif
}
}
}
}
Example:
import WebKit
...
WKWebview.clean()
Solution 4
I was able to solve this issue by having my WKWebView evaluate window.location.reload. I have a tap gesture recognizer on the web view and whenever it detects a double tap with 2 touches, I run the following:
webView.evaluateJavaScript("window.location.reload(true)", completionHandler: nil);
Hope this helps.
Solution 5
You cannot delete caches only with NSURLCache. After much cut-and-try, I could clear caches by the following steps (as of iOS 8.1.1):
- Use NSURLCache to delete caches in the same way as you used to do on UIWebView. If you use WKProccessPool, re-initialize it.
- Delete
Caches
directory inLibrary
. - Delete all WKWebViews
Next time you create WKWebView, caches should be cleared.
![Sravan](https://i.stack.imgur.com/YQ8Gw.png?s=256&g=1)
Author by
Sravan
Updated on July 08, 2022Comments
-
Sravan almost 2 years
Any one worked with
WKWebview
and tried to clear cache ? If yes, how to do? any example ?P.S. : Normal NSURLCache is not working.