清除WKWebView cookies
2016-11-11 15:50
363 查看
在UIWebView下,可以使用
[objc] view
plain copy
[[NSURLCache sharedURLCache] removeAllCachedResponses];//清除缓存
来实现清除缓存,但当替换使用WKWebView后,这个方法并不生效了(据说WKWebView不支持,我没找到官方说法~)
不过寻找了一下解决方法,分享一下
--------------IOS9以上----------------
[objc] view
plain copy
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以上---------------
[objc] view
plain copy
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *cookiesFolderPath = [libraryPath stringByAppendingString:@"/Cookies"];
NSError *errors;
[[NSFileManager defaultManager] removeItemAtPath:cookiesFolderPath error:&errors];
[objc] view
plain copy
[[NSURLCache sharedURLCache] removeAllCachedResponses];//清除缓存
来实现清除缓存,但当替换使用WKWebView后,这个方法并不生效了(据说WKWebView不支持,我没找到官方说法~)
不过寻找了一下解决方法,分享一下
--------------IOS9以上----------------
[objc] view
plain copy
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以上---------------
[objc] view
plain copy
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *cookiesFolderPath = [libraryPath stringByAppendingString:@"/Cookies"];
NSError *errors;
[[NSFileManager defaultManager] removeItemAtPath:cookiesFolderPath error:&errors];
相关文章推荐
- 清除WKWebView cookies
- 清除WKWebView cookies
- 清除WKWebView cookies的方法
- IOS WKWebView--清除缓存、弹窗处理
- iOS9 WKWebView清除缓存方法
- 清除WKWebView的缓存
- Swift WKWebView Cookies
- 清除WKWebView的缓存
- 浅谈iOS中WKWebView和UIWebView的清除缓存问题
- Swift3.0 设置WKWebView cookies
- 清除WKWebView的缓存
- iOS11.3 WKWebView清除cookie所踩的坑!
- WebView清除cookie
- WKWebView
- 论坛源码推荐(10.22):使用iOS8 WKWebView的浏览器模块,脉冲动画层
- ios webview清除缓存
- WKWebView使用说明
- WebView清除缓存
- 学习WKWebView控件
- WebView Cache 缓存清除