如何清除UIWebView缓存?

Mik*_*hev 25 iphone uiwebview

我需要UIWebView来显示一些本地.webarchive文件.但是那里的图像具有相同的名称,因此UIWebView始终只显示一个图像.如何清除缓存?

提前致谢

小智 32

// Flush all cached data
[[NSURLCache sharedURLCache] removeAllCachedResponses];
Run Code Online (Sandbox Code Playgroud)


Bir*_*chi 17

NSURLRequest* request = [NSURLRequest requestWithURL:fileURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0];

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

(现在已修复拼写错误)

  • 我在这里遇到同样的问题,所以我尝试了这个,不幸的是这不起作用.我怀疑它只是实际的html文件本身被加载忽略缓存.我们需要一些方法来使页面本身加载文件以忽略缓存... (7认同)

Tom*_*ift 11

我自己有这个问题,脚本和css文件被缓存.

我的解决方案是在src url上放置一个缓存清除参数:

<script src='myurl?t=1234'> ...
Run Code Online (Sandbox Code Playgroud)

其中t = 1234每次加载页面时都会更改.


jgo*_*zco 11

运用

[[NSURLCache sharedURLCache] removeAllCachedResponses];
Run Code Online (Sandbox Code Playgroud)

在进行调用之前或加载视图控制器时,这是一个静态函数,用于删除响应的所有缓存数据.只使用了cachePolicy,我没看到,我看到uiwebview可以刷新html文档,但不能刷新CSS,JS或图像等链接文档.

我尝试这个解决方案,它的确有效.

 if (lastReq){
    [[NSURLCache sharedURLCache] removeCachedResponseForRequest:lastReq];
    [[NSURLCache sharedURLCache] removeAllCachedResponses];

}

lastReq=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:localUrl]
                                   cachePolicy:NSURLRequestReloadIgnoringCacheData
                               timeoutInterval:10000];
[lastReq setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];

[self.mainWebView loadRequest:lastReq];
Run Code Online (Sandbox Code Playgroud)

- 首先,我删除了最后一个请求的缓存并调用删除所有其他缓存(我认为这不是那么重要) - 然后我创建一个nsurlrequest并忽略LocalCacheData - 最后,加载新请求

我检查它并重新加载所有文件(html,css,js和图像).


Nav*_*han 5

//to prevent internal caching of webpages in application
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];
sharedCache = nil;
Run Code Online (Sandbox Code Playgroud)

尝试使用这个.它将清除应用程序的url缓存内存.

  • 这是唯一对我有用的方法 - 由于某种原因`removeAllCachedResponses`什么也没做. (2认同)