I know that there are some caching classes introduced in the iphone sdk recently, and there is also a TTURLRequest from three20's library that allows you to cache a request to a URL. However, because I am loading the web page in UIWebView by calling UIWebView's loadRequest, those techniques are not really applicable.
Any ideas how I can save a web page so that on next app launch, I don't have to fetch from the web again for the full page? The page itself already have some ajax mechanism that updates parts of itself automatically.
I recently found this project under github : http://github.com/rs/SDURLCache The approach is quite the same as my previous answer described here How to save the content in UIWebView for faster loading on next launch? , but the code looks more polished so maybe it makes sense to give it a try.
There are a bunch of articles about the way the cache of the UIWebView works and the global feeling is that even if some mechanisms seems to work OK under MacOS X, the same approaches may have curious behavior under iPhone.
HOWEVER, I'm doing it by playing with the global cache that is accessed by any
NSURLConnection
,UIWebView
included. And in my case, it works ;).What you need to understand is the global flow:
loadRequest
on aUIWebView
NSURLCache
to ask "is there something cached for this request?":From that, here's what I do to handle the cache on the disk, on my side, to speed up the load of a UIWebView:
NSURLCache
and override the get control over the-(NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
selectorNow the code :
MyCache.h
MyCache.m
And the use of it in your application:
This code deserves a lot of cleanup.. but the main things should be in there. I had a lot of trouble to get this working, hope this helps.
Take a look at: http://allseeing-i.com/ASIHTTPRequest/ASIWebPageRequest
If the page has AJAX already, why not store the JavaScript/HTML in the application bundle to start rather than downloading it on the first launch? Then load the page with the code Corey gave below and let the AJAX handle hitting the network for the updated parts of the page.
You can save an HTML in the documents directory and load the page directly from the documents directory on launch.
To save the webview content: Reading HTML content from a UIWebView
To load: