首页 > 代码库 > 清除WKWebView的缓存
清除WKWebView的缓存
清除WKWebView的缓存
OC写法: swift写法再下下面。
清除WKWebView的缓存,让H5页面一刷新就更新至最新的页面
直接上代码~!~~
要区分iOS9.0和8.0两种
- (void)deleteWebCache {
if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) {
NSSet *websiteDataTypes
= [NSSet setWithArray:@[
WKWebsiteDataTypeDiskCache,
//WKWebsiteDataTypeOfflineWebApplicationCache,
WKWebsiteDataTypeMemoryCache,
//WKWebsiteDataTypeLocalStorage,
//WKWebsiteDataTypeCookies,
//WKWebsiteDataTypeSessionStorage,
//WKWebsiteDataTypeIndexedDBDatabases,
//WKWebsiteDataTypeWebSQLDatabases
]];
//// All kinds of data
//NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
//// Date from
NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
//// Execute
[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
// Done
}];
} else {
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *cookiesFolderPath = [libraryPath stringByAppendingString:@"/Cookies"];
NSError *errors;
[[NSFileManager defaultManager] removeItemAtPath:cookiesFolderPath error:&errors];
}
}
swift:
// MARK: - 清空缓存
func ClearCache() {
let dateFrom: NSDate = NSDate.init(timeIntervalSince1970: 0)
if #available(iOS 9.0, *) {
let websiteDataTypes: NSSet = WKWebsiteDataStore.allWebsiteDataTypes()
WKWebsiteDataStore.defaultDataStore().removeDataOfTypes(websiteDataTypes as! Set<String>, modifiedSince: dateFrom) {
print("清空缓存完成")
}
} else {
let libraryPath = NSSearchPathForDirectoriesInDomains(.LibraryDirectory, .UserDomainMask, true)[0]
let cookiesFolderPath = libraryPath.stringByAppendingString("/Cookies")
let errors: NSError
try? NSFileManager.defaultManager().removeItemAtPath(cookiesFolderPath)
}
}
清除WKWebView的缓存