Webview:缓存Ajax调用

I would like to build a native iOS app with a simple UIWebView, but cache all the stuff that goes through the Network.

For the first shot, I will include a button to invalidate the cache and on the next startup of the app, the cache will get refilled. Later I will include a call to the server, which then tells me to invalidate and refill the cache.

I have the following setup:

  • one UIWebView that loads an index.html file
  • index.html holds some CSS ans some JavaScript
  • further code (JS/CSS) is loaded via AJAX calls
  • tried to use the RNChachingURLProtocol from Rob Napier BUT it caches only URL Request (the CSS and Javascripts declared in the index.html), but none of the CSS and JS that gets loaded via AJAX.

Is there any library capable of caching also AJAX requests?

I think your problem is that these ajax calls are getting a response with http header parameters that specify that it must not be cached. You could create a custom NSUrlCache that override these parameters. A description of the NSUrlCache can be found at http://nshipster.com/nsurlcache/ and a sample can be found at http://github.com/evermeer/EVURLCache

Problem solved.

By reading this http://nshipster.com/nsurlcache/ (thanks Edwin!) and hook into the app on iOS Simulator via WireShark I experienced the following:

The Webview automatically caches most of the data automatically. My initial request was followed by 160 HTTP requests made to the server.

Further requests were about 25 HTTP requests.

So in my case I do not need any further Library to handle this specific usecase of caching (almost) all content, at some point remove all cached content and refill.