I am having trouble with my custom internet browser. I am using WKWebView. I have tabs in my app. If I click on a tab new NSURLRequest loads in the WKWebView. I need to impl
I typically will load from a server if I have internet connectivity. Otherwise, I load from the cache.
if reachability.isReachable {
urlRequestCache=NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.UseProtocolCachePolicy, timeoutInterval: 10)
}
else {
urlRequestCache = NSURLRequest(URL: url!, cachePolicy: NSURLRequestCachePolicy.ReturnCacheDataElseLoad, timeoutInterval: 60)
}
theWebView.loadRequest(urlRequestCache)
If you used NSURLRequestUseProtocolCachePolicy
, which is the default, there shouldn't be anything else you need to do. This policy will automatically look at the response from the server to decide whether or not it should actually go and grab the data again.
If the server uses Cache-Control HTTP headers and sets the max age for its responses, NSURLSession will respect this and return cached responses before they expire.