December 14, 2008

Caching eBooks for offline use on the iPhone

The esteemed fellow Ximianite Dave Camp pinged me to offer a solution to the problem I was kvetching about in my last writeup. He noted the following solution, which requires a web server to work - but if you're using a Mac, you can start up web sharing and hit it locally over your wifi. Anyway, in a folder on the web server, place an index file (let's say, index.html) with links to the various ebook files. These files need to be contained within the same folder (although they can be in subdirectories). At the top of the file, in the html tag, place the following:
<html manifest="books.offlineManifest">
  <!--- your page's html--->
Now create a file named (you guessed it) books.offlineManifest. Place this file in the same directory (at the same level) as index.html. Now the tricky part - modify your web server configuration so that files with the suffix ".offlineManifest" get served not as text/html but as text/cache-manifest. If you're using apache, the command you need to put into the configuration file looks like this:

AddType text/cache-manifest .offlineManifest

Restart the server if necessary to reload the config.

Now, inside the books.offlineManifest file, place the following:


Note that the book-title-1.html and the like are, of course, the location of the files that are pointed to by links on your index.html page. These files can be in subdirectories, but be careful - the cache manifest should not contain lines describing the subdirectories, just the files. So for example, do not do the following>

...the 'books/' line will cause the caching process to puke and stop. I mention this because, of course, I did it, being lazy and using 'find .' to get my tree info. DON'T BE ME.

All right, once you've got all these files saved to your webserver, use your iPhone and navigate to the index page. Once you've loaded it, click the '+' in Safari and seelct 'Add to Home Screen.' This will create an applet on the iPhone which loads that index. Quit Safari and then click that applet. It will reload Safari and bring up your index page. It will also start traversing the file list in books.offlineManifest and caching them onto your iPhone! Once it has done that, you'll be able to navigate to them using Safari even if the iPhone is not on the network, because Safari will realize it has them cached.

The down sides:

Well, yeah, there are some. For one, it means stuffing an awful lot of things into your cache. I don't know how that will affect usability. This feature is a) intended for smallish javascript files and the like, not megabyte+ book files, so YMMV. Also, as far as I know, it's an undocumented feature in Safari, so expect that Apple can change how it works or the fact that it's there at any time.

The most annoying thing, for me, is that it picks some strange order of files in my (really long, because I use exploded-by-chapter HTML books) file list. And, it never seems to finish loading my manifest - usually, the phone sleeps, or something silly, or it just stalls - and when it restarts (because I re-navigate to the page) it... starts loading them all over again. It might be most effective to have a manifest and applet per book or per author rather than having everything stuffed into one file. I found that when I limited the manifest to around twenty files, they all loaded fine. There is, however, no real way of telling what's loaded and how far it's gotten, because it's supposed to be invisible to the user (I figured it out by tailing the logs on my webserver while it was caching).

So there you have it. Thanks Dave Camp! Until the Safari file:// patch works on the 2.2 firmware, this is what I'm going to have to do. Posted by jbz at December 14, 2008 12:58 AM | TrackBack

Post a comment

Remember personal info?