Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear UIWebView cache when use local image file

Tags:

I use a UIWebView to load a local html, and there is a PNG file inside the html created by Objc.

After the PNG file has been modified, I reload the html in UIWebView, but the image doesn't change. However, if I quit the app and reopen it, the image file will be changed to the new one.

I have checked the PNG file in Documents with Finder, so I'm sure it has been modified, but the old one is still in UIWebView.

So, as I think that it's a UIWebView cache problem, I've tried:

  • [[NSURLCache sharedURLCache] removeAllCachedResponses];

  • [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:url isDirectory:NO ] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:1]]; or NSURLRequestReloadIgnoringCacheData

None of them works, and I can't change the filename, because the PNG file is used in a lot of places (other html and objc code).

I've tried this too: some.png?r=randomNumber but it can't be showed.

How do I clear the UIWebView cache when using a local image file inside a local html?

like image 626
yellow Avatar asked Feb 05 '13 17:02

yellow


2 Answers

Other than renaming every file on each access, I've only seen one thing work for this and that is modifying the HTML with javascript to add a timestamp onto the image url so it tricks the webview into thinking it's a different file. Images (usually) load the same no matter what you put after the ? in their url. I think this would be easier than renaming every file each time you load the web view. Something like this (using jQuery):

<img src="myimg.jpg" />

<script>
$(function() {
    $('img').each(function(i, el) {
        $(el).attr('src', $(el).attr('src')+'?pizza='+(new Date()).getTime());
    });
});
</script>

I guess this is assuming that this javascript loads and runs before the images are loaded, but in theory this should work.

For a little background, I've made a page in a webview that used RequireJS to asynchronously load quite a few files. I had the EXACT same problem that this question is talking about except that I was loading javascript files instead of images. The key to fixing this issue was adding a timestamp to every path of javascript file and thus tricking the webview (ex me.js?ts=236136236 and whatever.js?ts=3153524623). I found this to work great.

One other thing I needed to do was add a timestamp to the path of my HTML file when loading it into the webview like this:

[NSURL URLWithString:[[NSString stringWithFormat:@"%@/index.html?pizza=%f", webDirectoryPath, [[NSDate date] timeIntervalSince1970]]

I now can modify all the local files and each time the webview appears the changes come through.

like image 77
Benjamin Oman Avatar answered Sep 23 '22 13:09

Benjamin Oman


You can try this, in your AppDelegate.m

+(void)initialize {
  [[NSURLCache sharedURLCache] setDiskCapacity:0];
  [[NSURLCache sharedURLCache] setMemoryCapacity:0];
}
like image 38
oiledCode Avatar answered Sep 21 '22 13:09

oiledCode