Is there a way to request the HTML5 application cache?

Is there a way to request the contents of the HTML5 application cache?

I am writing an iOS application that uses a lot of cached web content. Before loading this page when the application is disabled, I want to check if the page exists in the cache. If this is not the case, I will inform the user that they must be online to see this content; if this happens, I will continue and download it.

IOS now has its own URL caching system, and I initially suggested that I could check the cache contents as follows:

if ([[NSURLCache sharedURLCache] cachedResponseForRequest:myRequest] != nil) { // go ahead and load the page } else { // notify the user that the content isn't available } 

Stupid to me. It seems that the iOS cache and HTML5 cache are not connected: -cachedResponseForRequest: returns nil for any request, even when I see that the URL is in the HTML5 application cache (using the Safari web debugger).

So, is there a way I can request the contents of the HTML5 application cache? It doesn't matter if the response uses Objective-C or Javascript code, since I can always just execute the appropriate JS from Objective-C.

+4
source share
2 answers

There are two HTML5 AppCache properties that mean that there shouldn’t be a need for normal operation:

  • AppCache update operations are atomic, either the entire cache is updated, or not one of them
  • After creating AppCache, all files in the cache will be sent from the cache

The end result is that for any given version of the manifest file, any file specified in it that is loaded into the browser will correspond to all other files specified in the manifest. All you need to check is window.applicationCache.status and check that it is not UNCACHED .

There is one more possibility. If you are “lazily adding” files to AppCache as described in Dive Into HTML5 , you may not be sure which files are cached. In this case, you could adapt one of the approaches to detecting online status , I'm not going to give you a fully tested solution, but here is a general idea:

  • Create a web page containing a unique identifier that is unlikely to ever appear on the page. The identifier may be in hidden content on a regular page.
  • Set this page as a common FALLBACK in the manifest.
  • Request pages using AJAX.
  • Scan the response for a unique identifier, if you find it, then you know that the requested page is not in AppCache
+2
source

Yes, the cache is stored in Application.db.

+1
source

All Articles