删除UIWebView的内部缓存 [英] Remove UIWebView's internal cache

查看:100
本文介绍了删除UIWebView的内部缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UIWebView 中显示一个网络应用程序,有时页面的内容会改变。内容更改后,应用程序将清除缓存。但是当我访问一个页面时,我之前访问过 UIWebView 不会发送HTTP GET请求,而是从缓存加载,即使我已禁用缓存,像这样:

  [[NSURLCache sharedURLCache] removeAllCachedResponses]; 
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];

最初我加载一个请求与cachePolicy cachePolicy:NSURLRequestReturnCacheDataElseLoad

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



有没有办法清除 UIWebView 的内部缓存?我甚至重新创建 UIWebView ,但缓存仍然存在。

解决方案

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



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

  NSString * testURL = [NSString stringWithFormat:@%@?t =%@,url,randQuery] ; 
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:testURL] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0]];

您可以生成随机字母数字字符串作为您的randQuery查询参数,或保留一个持续计数,只是计数。



这将强制UIWebView从远程资源加载。


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];

Initally I'm loading a request with cachePolicy cachePolicy:NSURLRequestReturnCacheDataElseLoad.

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:myURLString] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10.0]];

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.

Is there any way to clear the internal cache of UIWebView? I'm even recreating the UIWebView but the cache is still there.

解决方案

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.

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]];

where you generate a random alphanumeric string as your randQuery query parameter, or keep a persistent count and just count up.

This should force the UIWebView to load from the remote resource.

这篇关于删除UIWebView的内部缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆