iOS WKWebView缓存清理
1、在发起请求时直接从原始地址请求,不读缓存数据 : NSURLRequestReloadIgnoringCacheData
self.webView = [[WKWebView alloc]initWithFrame:f configuration:configuration];
_webView.navigationDelegate = self;
_webView.backgroundColor = [UIColor clearColor];
_webView.allowsBackForwardNavigationGestures =YES;//打开网页间的 滑动返回
_webView.scrollView.decelerationRate = UIScrollViewDecelerationRateNormal;
//监控进度
[_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
[_webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
[self.view addSubview:_webView];
//进度条
_progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
_progressView.tintColor = _progressViewColor;
_progressView.trackTintColor = [UIColor clearColor];
_progressView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 3.0);
[_webView addSubview:_progressView];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:_url] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:15.0];
//加密header部分
NSString *headerContentStr = [[HeaderModel new] modelToJSONString];
NSString *headerAESStr = aesEncrypt(headerContentStr);
[request setValue:headerAESStr forHTTPHeaderField:@"header-encrypt-code"];
[_webView loadRequest:request];
2、清除缓存
if(@available(iOS 9.0, *)) {
NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes];
NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0];
[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{
NSLog(@"清楚缓存完毕");
}];
}else{
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
}