Clear the UIWebView cache when using the local image file


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?

You can try this, in your AppDelegate.m

+(void)initialize {
  [[NSURLCache sharedURLCache] setDiskCapacity:0];
  [[NSURLCache sharedURLCache] setMemoryCapacity:0];