最近在做公司内部的一款app,涉及到UIWebview的缓存问题,
不是主动做的webview crach缓存,而是UIWebview的自动缓存,这个问题导致,网页更新的时候,即使在网址后面加了时间戳,也不能及时更新网页,
后来才知道要在离开网页的时候清理webview的缓存 并将webview置nil
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
_webView = nil;
[self cleanCacheAndCookie];
}
- (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];
}