I have a simple app loading a site optimized for the iPhone in a UIWebView
.
Problem is, caching does not seem to work:
[webView loadRequ
The ihone has limited caching capacity compared to a normal computer. It limits uncompressed cache items to 25k.
Good info here: http://yuiblog.com/blog/2008/02/06/iphone-cacheability/
You can now try ASIWebPageRequest by All Seeing Interactive:
ASIWebPageRequest is a new experimental addition to the ASIHTTPRequest family. It can be used to download a complete webpage, including external resources like images and stylesheets, in a single request. Once a web page is downloaded, the request will parse the content, look for external resources, download them, and insert them directly into the html source using Data URIS. You can then take the response and put it directly into a UIWebView / WebView on Mac.
I can only advise everyone to use Ben Copsey's great library for all sorts of HTTP operations anyways.
UPDATE: Ben has discontinued ASIHTTPRequest. I no longer suggest using it.
One workaround of this problem as I see is to
1) download HTML code
2) store it in the string
3) find all external links in it like
<img src="img.gif" width="..." height="..." />
4) download them all
5) replace them with embedded Base64-encoded version
<img src="data:image/gif;base64,R0lGODlhUAAPA...JADs= " width="..." height="..." />
6) finally store complete HTML with embedded images as you want.
From https://github.com/phonegap/phonegap-iphone/issues/148:
NSURLCache* cache = [NSURLCache sharedURLCache];
[cache setMemoryCapacity:4 * 1024 * 1024];
[cache setDiskCapacity:512*1024];
[NSURLRequest requestWithURL:appURL
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:10.0];
You should be able to subclass NSURLCache
and substitute it for the shared cache used by the UIWebView
as described in this Cocoa with Love article: Substituting local data for remote UIWebView requests
For another approach have a look at Drop-in offline caching for UIWebView (and NSURLProtocol).
You can always perform the requests manually, though that'll be tricky - and then you can cache things to your heart's content. Build a UIWebViewDelegate
that starts the request in webView:shouldStartLoadWithRequest:navigationType:
, cache the result, and use the UIWebView's loadHTMLString:baseURL:
to update the view.
It'll be ugly, and things won't work as smoothly as you might want, but it may be good enough for what you need.