问题描述
我在 UIWebView
中显示了一个网络应用程序,有时页面的内容会发生变化.内容更改后,应用程序会清除缓存.但是当我进入我之前访问过的页面时,UIWebView
不会发送 HTTP GET 请求,而是从缓存加载,即使我已经禁用了缓存:
I'm showing a web app in an UIWebView
, and sometimes the content of pages will change. After content have been changed the app clears the cache. But when I go to a page I've previously visited the UIWebView
doesn't send a HTTP GET request, but loads from cache even though I've disabled cache like so:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
最初我使用 cachePolicy cachePolicy:NSURLRequestReturnCacheDataElseLoad
加载请求.
Initally I'm loading a request with cachePolicy cachePolicy:NSURLRequestReturnCacheDataElseLoad
.
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:myURLString] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10.0]];
UIWebView
有某种内部缓存.已经访问过的页面将从这个内部缓存加载,而不是通过 NSURLCache
并且没有请求发送.
UIWebView
have some kind of internal cache. Already visited pages will be loaded from this internal cache instead of going through NSURLCache
and also there's no request sent.
有什么办法可以清除UIWebView
的内部缓存?我什至重新创建了 UIWebView
但缓存仍然存在.
Is there any way to clear the internal cache of UIWebView
? I'm even recreating the UIWebView
but the cache is still there.
推荐答案
看起来这里发生的是它重新加载实际的 HTML 文件,但不一定重新加载该页面中的资源.
It appears that what's happening here is that it reloads the actual HTML file, but does not necessarily reload the resources within that page.
我见过的一个可能的解决方案是将查询参数附加到 URL 的末尾.例如:
A possible solution I've seen is to append a query parameter on to the end of the URL. For example:
NSString *testURL = [NSString stringWithFormat:@"%@?t=%@", url, randQuery];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:testURL] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0]];
在其中生成 随机字母数字字符串作为 randQuery 查询参数,或者保持一个持久的计数,然后向上计数.
where you generate a random alphanumeric string as your randQuery query parameter, or keep a persistent count and just count up.
这应该强制 UIWebView 从远程资源加载.
This should force the UIWebView to load from the remote resource.
这篇关于移除 UIWebView 的内部缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!