问题
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.
回答1:
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.
回答2:
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*
来源:https://stackoverflow.com/questions/6542114/clearing-a-webviews-cache-for-local-files