iOS webview清除缓存

使用ios的webview会自动进行缓存,我们在开发的时候要记得清除Cookie和缓存。

在webview的关闭按钮中添加两个方法

/**webView退出方法*/
- (void)closeBtnAction:(UIButton *)button{
     _webView = nil;
    [self cleanCacheAndCookie];
    [self.navigationController popViewControllerAnimated:YES];
}

/**清除缓存和cookie*/
- (void)cleanCacheAndCookie{
    //清除cookies
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies]){
        [storage deleteCookie:cookie];
    }
   //清除UIWebView的缓存
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    NSURLCache * cache = [NSURLCache sharedURLCache];
    [cache removeAllCachedResponses];
    [cache setDiskCapacity:0];
    [cache setMemoryCapacity:0];
}



版权声明:本文为IT_201607原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。