在UIWebView下,可以使用
[[NSURLCache sharedURLCache] removeAllCachedResponses];//清除緩存
來實現清除緩存,但當替換使用WKWebView後,這個方法並不生效了(據說WKWebView不支持,我沒找到官方說法~)
不過尋找了一下解決方法,分享一下
--------------IOS9以上----------------
WKWebsiteDataStore *dateStore = [WKWebsiteDataStore defaultDataStore];
[dateStore fetchDataRecordsOfTypes:[WKWebsiteDataStore allWebsiteDataTypes]
completionHandler:^(NSArray<WKWebsiteDataRecord *> * __nonnull records) {
for (WKWebsiteDataRecord *record in records)
{
// if ( [record.displayName containsString:@"baidu"]) //取消備註,可以針對某域名清除,否則是全清
// {
[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:record.dataTypes
forDataRecords:@[record]
completionHandler:^{
NSLog(@"Cookies for %@ deleted successfully",record.displayName);
}];
// }
}
}];
但顯然大多小夥伴們都需要支持IOS8的,所以尷尬的是上面的方法暫時用不到,所以還是老老實實的這麼幹XD
--------------IOS8以上---------------
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *cookiesFolderPath = [libraryPath stringByAppendingString:@"/Cookies"];
NSError *errors;
[[NSFileManager defaultManager] removeItemAtPath:cookiesFolderPath error:&errors];
- -+