Clear web browsing cache for local files

I found a few similar posts, but they don't seem to affect what I'm doing. I have a UIWebView that I use to display local content in my package. In particular, I am showing a docx file. The user rarely has to ever look at this document, and my application is closely related to memory, so I want UIWebView not to cache the document. Another viable option is to clear the cache when the user leaves the view. I completely agree with the need to download it from scratch every time a user enters a view.

I load the document like this:

 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 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 a policy for caching web views, but it seems to be just memory, since when I released the web view, there was still memory. When re-entering and reusing, he did not use the same memory.

I do not think this is the right direction. All of them have been flagged as ways to stop web pages from caching, but they do not seem to affect local files. I really don't know where else to go with this. If someone knows how to make the cache clear or prevent caching, I would really appreciate help.

+8
ios uiwebview nsurlcache
source share
2 answers

WebKit supports its own caches in addition to the standard NSURLCache . You do not control it.

Actually, you need to make sure your code behaves well under low memory warnings, and consider that UIWebView will do the same.

0
source share

I'm not sure if your problem was loading cached data or was it simple that the last requested URL was cached and therefore did not start a new downloadable document. It seemed to me, so I used this very dirty trick:

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

And for every new web request:

 - (void)webViewLoadURL:(NSURL *)url { [[_webView mainFrame] loadRequest:_urlRequestCacheBust]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10]; [[_webView mainFrame] loadRequest:urlRequest]; } 

* Shiver *

0
source share

All Articles