清除本地文件的webviews缓存 [英] clearing a webviews cache for local files

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

问题描述

我发现了一些类似的帖子,但它们似乎对我正在做的事情没有任何影响。我有一个 UIWebView ,我用它来显示我的包中的本地内容。具体来说,我正在显示一个docx文件。用户应该很少看到这个文档,我的应用程序内存很紧,所以我想做的是阻止 UIWebView 缓存文档。另一个可行的选择是在用户离开视图时清除缓存。每次用户进入视图时,我都必须从头开始加载它。

I've found some similar posts, but they don't seem to have any effect with what I'm doing. I've got a UIWebView that I'm using to display local content in my bundle. Specifically I'm displaying a docx file. The user should rarely ever look at this document, and my app is tight on memory, so what I want to do is prevent the UIWebView from caching the document. Another viable option is to clear the cache when the user leaves the view. I'm totally ok with having to load it from scratch every time the user enters the view.

我像这样加载文档:

NSString * path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"Intro text"] ofType:@"docx"];
NSURL * url = [NSURL fileURLWithPath:path];
request = [NSURLRequest requestWithURL:url];

doc_view_rect = CGRectMake(5,105,self.view.frame.size.width,self.view.frame.size.height);
doc_viewer = [[UIWebView alloc] initWithFrame:doc_view_rect];
[doc_viewer loadRequest:request];

在我尝试停止缓存时,我尝试过:

In my attempts to stop the caching I've tried:

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

并且:

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];
sharedCache = nil;

并且:

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

我也试过设置网络视图缓存策略,但这似乎只是吃了内存,就像当我发布了webview,还有留下的内存。当我重新输入并重新分配时,它没有使用相同的内存。

I also tried setting the web-views caching policy, but that just seemed to eat memory, as when I released the webview, there was still memory left behind. When I re-entered and realloced it didn't use the same memory.

我不认为这是正确的方向。这些都被标记为阻止网页缓存的方法,但它们似乎对本地文件没有影响。
我真的不确定这个去哪儿。如果有人知道如何强制缓存清除,或者首先阻止缓存,我会非常感谢一些帮助。

I don't think this is the right direction though. These were all tagged as ways to stop webpages from caching, but they seem to have no effect with local files. I'm really not sure where else to go with this one. If anyone knows how to force the cache to clear, or prevent caching in the first place I would greatly appreciate some help.

推荐答案

我不确定您的问题是加载缓存数据,还是简单说明上次请求的URL是否已缓存,因此不会触发要加载的新文档。这似乎是我的情况,所以我使用了这个非常脏的技巧:

I'm not sure if your problem was loading cached data, or simple that the last requested URL was cached, and therefore not triggering a new document to load. That seemed to be the case for me, so I used this very dirty trick:

_urlRequestCacheBust = [NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];

对于每个新的网络请求:

And for each new web request:

- (void)webViewLoadURL:(NSURL *)url
{
    [[_webView mainFrame] loadRequest:_urlRequestCacheBust];

    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
    [[_webView mainFrame] loadRequest:urlRequest];
}

* shudder *

*shudder*

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

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