删除UIWebView的内部缓存

Nik*_*und 14 cocoa-touch caching uiwebview nsurlcache ios

我正在展示一个网络应用程序UIWebView,有时页面内容会发生变化.更改内容后,应用程序将清除缓存.但是当我去一个我以前访问过的页面时,UIWebView它不发送HTTP GET请求,但是从缓存加载,即使我已经禁用了缓存,如下所示:

[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
Run Code Online (Sandbox Code Playgroud)

最初我正在使用cachePolicy加载请求cachePolicy:NSURLRequestReturnCacheDataElseLoad.

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:myURLString] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10.0]];
Run Code Online (Sandbox Code Playgroud)

UIWebView有某种内部缓存.已访问的页面将从此内部缓存加载而不是通过NSURLCache,也没有发送请求.

有没有办法清除内部缓存UIWebView?我甚至正在重新创建,UIWebView但缓存仍在那里.

Joe*_*her 21

看来这里发生的是它重新加载实际的HTML文件,但不一定重新加载该页面中的资源.

我见过的一个可能的解决方案是将查询参数附加到URL的末尾.例如:

NSString *testURL = [NSString stringWithFormat:@"%@?t=%@", url, randQuery];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:testURL] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0]];
Run Code Online (Sandbox Code Playgroud)

您在其中生成随机字母数字字符串作为randQuery查询参数,或者保持持久计数并且只是计数.

这应该强制UIWebView从远程资源加载.


小智 7

我有同样的问题,并将HTTPShouldHandleCookies属性设置为NO修复了我的问题.

例如:

NSMutableURLRequest  *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strurl]];

[request setHTTPShouldHandleCookies:NO];

[webView loadRequest: request];
Run Code Online (Sandbox Code Playgroud)

希望这有帮助.