Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove UIWebView's internal cache

I'm showing a web app in an UIWebView, and sometimes the content of pages will change. After content have been changed the app clears the cache. But when I go to a page I've previously visited the UIWebView doesn't send a HTTP GET request, but loads from cache even though I've disabled cache like so:

[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];

Initally I'm loading a request with cachePolicy cachePolicy:NSURLRequestReturnCacheDataElseLoad.

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:myURLString] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10.0]];

UIWebView have some kind of internal cache. Already visited pages will be loaded from this internal cache instead of going through NSURLCache and also there's no request sent.

Is there any way to clear the internal cache of UIWebView? I'm even recreating the UIWebView but the cache is still there.

like image 671
Niklas Berglund Avatar asked Oct 01 '13 10:10

Niklas Berglund


2 Answers

It appears that what's happening here is that it reloads the actual HTML file, but does not necessarily reload the resources within that page.

A possible solution I've seen is to append a query parameter on to the end of the URL. For example:

NSString *testURL = [NSString stringWithFormat:@"%@?t=%@", url, randQuery];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:testURL] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0]];

where you generate a random alphanumeric string as your randQuery query parameter, or keep a persistent count and just count up.

This should force the UIWebView to load from the remote resource.

like image 162
Joel Fischer Avatar answered Nov 05 '22 22:11

Joel Fischer


I had the same issue and setting HTTPShouldHandleCookies property to NO fixed my problem.

For example:

NSMutableURLRequest  *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strurl]];

[request setHTTPShouldHandleCookies:NO];

[webView loadRequest: request];

Hope this help.

like image 39
Katerina Avatar answered Nov 05 '22 21:11

Katerina