Hi
We are building web applications for the iPhone that work offline. But we are having difficulties caching dynamic images. Please read on and I'll show by example exactly what I mean and what we have done so far.
So for example let's say we are building a simple list application with just 1 page. The application's only purpose is to list 5 items, each item containing some text and 1 image.
The application has a simple logo and some separate JavaScript and CSS code. These static resources are cached using the cache manifest file.
There are 2 scenario's:
Scenario 1: I'm online and I open up the web application
When I load the list application in Safari it will fetch 5 new random items out of a database containing 1000's of items. These are all served by simple backend through a AJAX call (JSON format).
The whole JSON object containing the 5 items is immediately stored in HTML5 local storage and cached for offline usage.
Structure of the JSON object is a bit like this:
{
"0" : {
id: "3",
text: "Some text about item #3",
image_url: "http://www.domain.com/image22341.png"
},
"1" : {
id: "23",
text: "Some text about item #23",
image_url: "http://www.domain.com/image442321.png"
},
"2" : {
id: "4",
text: "Some text about item #4",
image_url: "http://www.domain.com/image2321.png"
},
"3" : {
id: "432",
text: "Some text about item #432",
image_url: "http://www.domain.com/image2441.png"
},
"4" : {
id: "43",
text: "Some text about item #43",
image_url: "http://www.domain.com/image221.png"
}
}
As you can see, very simple (may be some errors in that JSON), the whole JSON object is stored in local storage.
Now the 5 items are rendered using JavaScript injected HTML (styled using CSS), nothing fancy. Span tags containing the text and image tags pointing to the image resource are created etc.
In online mode, it all works great.
Scenario 2: I'm OFFLINE and I open up the web application
The page loads (the logo is displayed because it was cached as a static resource using the cache manifest), some JavaScript detects that we are indeed offline and the application as a result does not attempt to contact the backend. Instead it reads the previously stored JSON object from local storage and commences rendering the 5 items. All as anticipated.
Text is displayed fine, but this time, the images are not displayed, reason for that is simple, the image tags are pointing to image resources which are not available. So it displays that little image not available icon.
Now my question is, is there any way to cache those image resources somehow? So that next time we need them, they are fetched from cache.
This is what I've tried:
- Base64 encode the images and supply them through JSON. This works BUT, it dramatically increases both fetching and rendering time (we are talking about 30 seconds increase, very slow)
- Some cache manifest hacking/trial and error.. couldn't find anything that works (ideally need a policy that does 'cache all resources on domain as they are requested', but to my knowledge this does not exist)
I have literally spent hours on this and can't find a solution... does anyone have a clue? I know this is possible because if you look at the Google Mail HTML5 application for the iPhone, they can somehow cache the attachments and you can retrieve them even while offline.
The one thing we haven't tried is using the SQLite databases that are supported by Safari... maybe I could store the images as a BLOB (still means fetching it from the feed and thus slow?) and then somehow magically converting that into an image on the screen.... but I have no idea on how to do this.
Any help is appreciated, thanks.