21

I want to load a webview without cache?

//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Request Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[kdgWebView loadRequest:requestObj];
Cœur
  • 37,241
  • 25
  • 195
  • 267
Jean St John
  • 213
  • 1
  • 2
  • 5

4 Answers4

38

Objective-C:

// Remove and disable all URL Cache, but doesn't seem to affect the memory
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];

You can find more information in http://blog.techno-barje.fr/post/2010/10/04/UIWebView-secrets-part2-leaks-on-release/

Swift:

URLCache.shared.removeAllCachedResponses()
URLCache.shared.diskCapacity = 0
URLCache.shared.memoryCapacity = 0
jfornoff
  • 1,368
  • 9
  • 15
LuisEspinoza
  • 8,508
  • 6
  • 35
  • 57
  • Doesn't work for me ? cached data are loaded even if I put this in the code. – Jean St John Mar 28 '12 at 08:25
  • You can change the cache policy – LuisEspinoza Mar 28 '12 at 12:11
  • Thanks very much for the answer. This solution worked for me, where I am using a UIWebView and I didn't want cached pages loading when there is no Internet connection. – Jason TEPOORTEN Feb 23 '13 at 00:42
  • This doesn't work every time. It worked for about 5 tests then failed. I don't think it gets at all of the cached data. – Jim True Sep 02 '13 at 16:05
  • How do you change the Cache policy for a web view? – MarkM Feb 19 '15 at 15:07
  • For me this didn't work initially when my UIWebView was pointed at a localhost server running on top of https://github.com/indexzero/http-server. When I switched to using https://nodejs.org/api/http.html, then the code above fixed my caching issue. I was also able to break caching without the code above using the former http-server running `http-server -c-1`. Lesson learned: the caching behavior is based not just on your iOS code, but your web server as well. – Liron Yahdav Oct 15 '16 at 19:31
15

You can change your cache policy, using the code:

NSURLRequest *requestObj = [NSURLRequest requestWithURL:url 
                                        cachePolicy:NSURLRequestReloadIgnoringCacheData
                                    timeoutInterval: 10.0]; 
LuisEspinoza
  • 8,508
  • 6
  • 35
  • 57
  • thanks, I found the reason, why the cache was loading... I used a cache manifest, and there is a bug with the cache manifest in HTML5. – Jean St John Apr 11 '12 at 15:37
0

I try to answer a similar question here. I hope it was useful for you! The last solution works for HTML but not for images, css and js linked in html.

How to clear UIWebView cache?

Community
  • 1
  • 1
jgorozco
  • 593
  • 10
  • 11
0

I've tried all of these solutions but none of them work every single time. My main test was an html page with an image set as a background in the html page. This html page and image are downloaded locally on my iPad through a "sync" method i have written. The only way to keep the cache fully reset is the constantly change the url of the image in the html page so it thinks it's a unique image each time. For example in my html:

<body background="image.png?v123">

So each page change i make i have to update the version number:

<body background="image.png?v1234">

etc, etc. You can change the url in anyway you want, it just has to be different.

Well i'm tired of doing this. Now When i download content locally, I am instead dynamically changing the containing folder's name. Since this folder is a parent folder and all of my links are relative it means i don't have to edit my html files each time. So far it seems to be working ok. I wish these other methods were consistent as it would be so much easier to simply reset the cache.

So in my requests its the baseURL which is causing my cache to reset, not the query string to my url or background image, or css file, etc, etc.

NSString *myURL = @"index.html";
NSURL *baseURL = [NSURL fileURLWithPath:@"FULL_PATH_TO_DOCS/MYFOLDERV123"];
[myTargetWebView loadHTMLString:myURL baseURL:baseURL];
Jim True
  • 1,085
  • 12
  • 13