清除本地文件的 webview 缓存

发布于 2024-11-17 21:19:25 字数 1394 浏览 3 评论 0原文

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

我像这样加载文档:

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

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

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

我还尝试设置网络视图缓存策略,但这似乎会消耗内存,因为当我释放网络视图时,那里仍然是记忆留下的。当我重新输入并重新分配时,它没有使用相同的内存。

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

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.

I load the document like so:

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

And:

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

And:

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┼── 2024-11-24 21:19:25

除了标准的 NSURLCache 缓存之外,WebKit 还维护自己的缓存。你无法控制它。

实际上,您必须确保您的代码在低内存警告下表现良好,并相信 UIWebView 也会做同样的事情。

WebKit maintains its own caches in addition to the standard NSURLCache one. You have no control over it.

Realistically, you have to make sure your code behaves well under low memory warnings, and trust that UIWebView will do the same.

长梦不多时 2024-11-24 21:19:25

我不确定您的问题是否是加载缓存数据,或者只是缓存了最后请求的 URL,因此不会触发新文档的加载。对我来说似乎就是这种情况,所以我使用了这个非常肮脏的技巧:

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

对于每个新的网络请求:

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

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

*颤抖*

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*

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文