WKWebView清除缓存

一. UIWebView-清楚缓存

  • 在使用WKWebView之前使用的是UIWebView, 清除缓存的方式两种:
  1. NSURLCache 和 NSHTTPCookieStorage 对象的清除方式
 NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies]) {
        [storage deleteCookie:cookie];
    }
    NSURLCache *cache = [NSURLCache sharedURLCache];
    [cache removeAllCachedResponses];
    [cache setMemoryCapacity:0];
    [cache setDiskCapacity:0];

  1. NSURLRequest对象的不缓存机制:NSURLRequestReloadIgnoringCacheData

//NSURLRequest *urlRequest = [NSURLRequest requestWithURL:_url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:15.0];
//[self.web_view loadRequest:urlRequest];
[self.web_view loadRequest:[NSURLRequest requestWithURL:_url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0]];

二. WKWebView清除缓存

  • ios8 问世之后, 本作者立马想换掉老的UIWebView, WKWebView的好处这里就不介绍了. 由于项目已成型了且诸多JS交互, 与web人员沟通了一阵, 终于同意使用新控件.

  • 上面的(1) NSURLCache 和 NSHTTPCookieStorage 对象的清除方式对WKWebView没起到作用. 采用方式NSURLRequest对象的不缓存机制:NSURLRequestReloadIgnoringCacheData

[self.web_view loadRequest:[NSURLRequest requestWithURL:_url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0]];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容