UIWebView Cache Substitution

Now here’s a downright clever approach to the problem of slow network connections making your UIWebView display drag:

Substituting local data for remote UIWebView requests

In this post, I’ll show you how you can load a webpage in a UIWebView in iOS while using a modified NSURLCache to substitute local copies of resources within the webpage for the remote copies referred to by the actual page…

… The only important method we need to override is cachedResponseForRequest:. This will allow us to examine every request before it is sent and return local data if we prefer…

Neat idea! Also note that NSURLCache is used by NSURLConnection as well, so this same technique would no doubt be applicable to replacing the rather complex — and no doubt buggy somewhere — code that we’ve implemented in various apps for updating their local copies of web content.

And … why check this out, looks like some bright spark already had that idea:

Add on-disk HTTP caching in your iPhone / iPad application

… It’s a shame Apple didn’t let developers choose if they need needs on-disk caching or not. It’s why I found it necessary to give this feature back to the Cocoa Touch framework. I thus created the SDURLCache open-source projet for this purpose. It’s a complete wrapper around NSURLCache to add on-disk caching support back our apps…

So check out SDURLCache on github for what’s no doubt a good start on that!

Alex | September 6, 2010

Leave a Reply