9

In the iPad project I'm working on I've got a UIWebView inside of the app which displays a .html file which links to a .css file, i.e.

<link rel="stylesheet" href="style.css" type="text/css">

These files are stored locally on the iPad but are fetched from a remote server. I've noticed that UIWebView caches the .css file more or less indefinitely, and refuses to load the new file whenever I change it. I've once changed the name of the file just to get it to reset, but that's unacceptable in the long run.

Is there a way to prevent caching of the CSS file in a UIWebView? Or even better, is there a way to say when to cache and when not to?

Kalle
  • 13,186
  • 7
  • 61
  • 76

2 Answers2

16

Yes.

Change your line to this:

<link rel="stylesheet" href="style.css?version=1" type="text/css">

Every time that you update the stylesheet, change the version. The browser will think that it is a different page because of the query string, and your server will ignore it.

If you are using a server side language such as PHP, you can also do the following:

<link rel="stylesheet" href="style.css?version=<?php echo time(); ?>" type="text/css">

This will change the version every time you refresh, thus stopping all caching.

riwalk
  • 14,033
  • 6
  • 51
  • 68
  • That's a neat idea, for sure! But if I say I have something like 15 .html files which each have their own line, this would still not solve the problem. If it was only one file... – Kalle Aug 23 '10 at 16:55
  • Having 15 files with the exact same tag is a sign of duplication in your code. Remove duplication, then changing the version will be trivial. – riwalk Aug 23 '10 at 17:04
  • 4
    no server side languages on local files in iPad - only HTML, CSS and JavaScript. @Kelle, you might use @Stargazer712's time suggestion from Objective-C. Something like this: `[htmlString stringByReplacingOccurrencesOfString:@"style.css" withString:[NSString stringWithFormat:@"style.css?time=%@", [[NSDate date] description]]` – Michael Kessler Aug 23 '10 at 19:51
  • Stargazer712: why is having multiple .html files which each link to the same CSS file duplication in code? Duplication would be putting the CSS straight into the HTML file in a style tag. This is more like inheriting from the same class in a bunch of objects. Michael: yeah, I went with that route in fact. Thanks for the input, both of you. – Kalle Aug 24 '10 at 09:23
  • You answered your own question. Making a change such as this requires that you change 15 files. _That is duplication_. It doesn't sound like you have access to a server-side language, so this may not have a quick remedy, but if you do have access to a server-side language, you need to look into using templates or master pages (depending on the language of course). – riwalk Aug 24 '10 at 14:57
  • Neat. And this works very well for HTML loaded using loadHTMLString as you can insert the version parameter into the HTML string before loading it. – SarahR Feb 10 '15 at 03:18
  • I don't recommend templating on the web view level using JS, especially if if you're supporting less recent webviews. The less you hammer away at the DOM, the better with some of these renderers. – Erik Reppen Feb 25 '16 at 17:52
1

The better way that keeps your HTML markup and iOS code isolated is

[[NSURLCache sharedURLCache] removeCachedResponseForRequest:yourRequest];
jamesmoschou
  • 1,173
  • 8
  • 15
  • 1
    `[[NSURLCache sharedURLCache] removeAllCachedResponses];` That's to clear all. See ToddH's answer here for a way to also limit the amount of memory the caching is allowed to use. Really helps with webview perf. http://stackoverflow.com/questions/5468553/clearing-uiwebview-cache – Erik Reppen Feb 25 '16 at 17:44