Caching content for iPhone webapp

January 29th, 2010

This post is still in progress; I haven’t gotten caching to work well yet. Loading...Every time I load my iPhone webapp I have to wait for it to download ~100 images and buttons. I’ll be closing and opening this app lots, so I’m trying to turn local caching on for all these images.

This article has gotten me started.

First of all I generated a cache.manifest file. Ideally, I want to cache anything within specific folders (images folders) but I couldn’t see anything that lets you use wildcards in the manifest. I’ll come back to trying this once I get a basic cache working, so for the time being I used a string of UNIX commands to generated a list of the files in my image directories, and stick it in my base manifest file.

I would have used sed, but couldn’t find how to prepend each line. Instead I used a for loop. Note that the path printed in the cache.manifest file is a URL path from the manifest’s location.

for ff in ls ../../path/to/images; do echo /path/to/images/$ff >> cache.manifest; done;

The manifest file needs to be served as the text/cache-manifest mime type. My hosting is on GoDaddy’s shared linux service, and this godaddy help article explains (briefly) how to set mime types on my account.

My site-wide .htaccess file has lots of wordpress configuration, but adding the following to the top of the file worked.

AddType text/cache-manifest manifest

To test it worked, I used http://web-sniffer.net/ (Firebug’s net tab and webkit’s Resources tab didn’t show the manifest file)

Next, to test if it’s effective on my iPhone.

So far no effect :(

I found a fantastic resource about the cache, and this has helped me get it working. Sort of. http://building-iphone-apps.labs.oreilly.com/ch06.html

It’s not interacting well with the fact that the webapp is behind a username and password enforced using .htaccess. I moved the cached resources out of the password protected area, which got the caching to work. However – the iPhone seems to be refusing to connect, and relies entirely on the cache. Even when connected to the internet, resources not listed in the manifest file aren’t downloaded. The ajax requests have stopped working, too. The NETWORK: section doesn’t help.

Okay – it’s almost working, now :D Apple’s resource details that the NETWORK: section is a list of prefixes. Using this allowed the ajax requests to work (it asks for login on the first request). This means it’s caching the initial php load.

Links