I am attempting to cache a webpage within a UIWebView using NSURLRequestCachePolicy
UseProtocolCachePolicy
- however, I can validate from my network monitor that the UIWebView is carrying out a full reload on every request and never loading the stored cache.
There is an older question claiming that UIWebView ignores cache settings on any NSURLRequest given to it. However, this seems inconclusive at best.
Below are the response headers from the server - the most pertinent is Cache-Control: no-cache
, which requires clients to revlidate the cache before rendering; this appears acceptable as Apple's default caching behavior (shown below) seems to account for this.
Update
Updating the caching policy to ReturnCacheDataElseLoad
will cause the cache to be loaded - so it looks like UIWebview can indeed cache responses; however, this functionality returns a cache no matter how old it is and that is far from desired.
Swift Code
let url = "http://web.page.com"
let requestURL = NSURL(string: url)
let request = NSURLRequest(URL: requestURL!, cachePolicy: NSURLRequestCachePolicy.UseProtocolCachePolicy, timeoutInterval: 15.0)
webView.loadRequest(request)
Server Response Headers
Accept-Ranges → bytes
Cache-Control → no-cache
Content-Encoding → gzip
Content-Length → 54794
Content-Type → text/html
Date → Tue, 14 Jul 2015 18:50:22 GMT
ETag → "807677b3872d01:0"
Last-Modified → Wed, 08 Apr 2015 14:31:29 GMT
Pragma → no-cache
Proxy-Connection → Keep-alive
Server → Microsoft-IIS/7.5
Vary → Accept-Encoding
X-Powered-By → ASP.NET
X-Robots-Tag → noarchive
Apple's Default Caching Protocol