Clear UIWebView cache when use local image file
Asked Answered
B

3

21

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?

Brigantine answered 5/2, 2013 at 17:56 Comment(2)
Did the answer by Elio.d really solved it for you? didn't work for me.Apace
I too have this problem and NEED a solution!!!! - starting a bounty!Iman
V
2

You can try this, in your AppDelegate.m

+(void)initialize {
  [[NSURLCache sharedURLCache] setDiskCapacity:0];
  [[NSURLCache sharedURLCache] setMemoryCapacity:0];
}
Veratrine answered 9/2, 2013 at 0:33 Comment(2)
Doesn't work for me. The local image is never updated. And I have the same symptoms, when I qui the app and relaunch, the image is updated.Apace
I think it could help when you're working with remote data but not local. When you just loaded the local html file and it remains unchanged there will not actual reload of view contents by repeated request.Solis
W
4

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.

Whitleather answered 17/7, 2013 at 4:4 Comment(1)
still happens after more than 3 years, you saved the day!Zaporozhye
V
2

You can try this, in your AppDelegate.m

+(void)initialize {
  [[NSURLCache sharedURLCache] setDiskCapacity:0];
  [[NSURLCache sharedURLCache] setMemoryCapacity:0];
}
Veratrine answered 9/2, 2013 at 0:33 Comment(2)
Doesn't work for me. The local image is never updated. And I have the same symptoms, when I qui the app and relaunch, the image is updated.Apace
I think it could help when you're working with remote data but not local. When you just loaded the local html file and it remains unchanged there will not actual reload of view contents by repeated request.Solis
S
1

If your html didn't change and the only change was in image you should use UIWebView's reload message instead of loading request again.

Something like this:

- (void)reloadWebView:(id)sender
{
    if (self.loaded) {
        [self.webView reload];
    } else {
        NSString *html = [[NSBundle mainBundle] pathForResource:@"web" ofType:@"html"];
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:html]];
        [self.webView loadRequest:request];
        self.loaded = YES;
    }
}

You don't even need any manipulations with cache.

Solis answered 31/5, 2013 at 3:38 Comment(5)
That's not true, the only reason we are asking how to clear the cache is because we have already reloaded the html file and the images did NOT change. You are using the loadRequest method which actually has a way to clear the cache... I was using the loadHTML method which has no way to clear the cache (since my html file was being generated on the spot in a string)... I guess I'd have to save the string as an actually .html file in my bundle then use loadRequest with it's clear cache method.Iman
But according to the person who asked this question... even using the loadRequest's clear cache method... (cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData) it still didn't update the png files that were changed in the bundle from within the app!Iman
The key point is in the reload message. It's not an issue related to caching, because in my test project I didn't do anything with caches and all works fine. When I replace an old image with a new one and then call reload the image in the webview changed. I think it's not related to the way you load your data too.. But should to check it.Solis
Unfortunately I already tried reload and it didn't work /: As of now I've just ended up renaming all 108 images each time a change is made and then updating those new image name references throughout the whole project, then reloading them all and deleting the old images with the old names (each image is only like 8x8 pixels so it's not tooo bad)Iman
Well, I did the tests and have to say you're right. WebView doesn't refresh images when the html loaded from string. It's very strange behaviour.. Since it works as needed when you're loading file into WebView I think it's better to use this approach instead of yours. It would be much easier to save string in the Temp directory and just reload WebViews when you've change your images.Solis

© 2022 - 2024 — McMap. All rights reserved.