NSURLRequest.CachePolicy

According to the documentation https://developer.apple.com/documentation/foundation/nsurlrequest.cachepolicy if I'm using the

NSURLRequestUseProtocolCachePolicy
option and the response stale, system issues a HEAD request. But if there is no internet connection, this request could not be completed so NSURLRequest cannot determine whether the HEAD changed and decide should it return the cached response.

Is there a way to force NSURLRequest to use the cached response if there is no connection and cache policy is

NSURLRequestUseProtocolCachePolicy
?

Is there a way to force NSURLRequest to use the cached response if there is no connection and cache policy is

NSURLRequestUseProtocolCachePolicy
?

No. If you specify the protocol cache policy you get its cache behaviour; if you need a different cache behaviour, use a different cache policy.

It sounds like you’re trying to support some sort of offline mode here. If so, be aware that trying to support on offline mode via the NSURLCache general doesn’t end well. NSURLCache was designed as a general purpose cache and doesn’t have the infrastructure needed to reliably support an offline mode.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks for reply.

You're right, I want to make some of our webpages available offline via WKWebView. Could you please give me any ideas on how to implement it?

You're right, I want to make some of our webpages available offline via WKWebView. Could you please give me any ideas on how to implement it?

My general recommendation is that you mirror your web site to the file system and then load it using

-loadFileURL:allowingReadAccessToURL:
. Be warned, however, that this is not easy. Most web sites are structured assuming that the Internet is available and those assumptions break when you run them this way. Making your web site work nicely when offline will require coördination between you and the web site developer. The alternative is to implement a bunch o’ client side hacks, which isn’t a good option in the long term.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I'm interested in the lifetime of cached data stored with these two implementations. What's the difference?

A cache that’s specifically designed to support offline mode would not be a cache. A cache, by definition, is a “software component that stores data so that future requests for that data can be served faster”. That gives the cache lots of flexibility in how it’s implemented.

Supporting offline mode is not about making things faster [1], it’s about making things work at all. Such a structure would have to provide guarantees that are not related to performance but are required for correctness. For example, there would have to be a way to force data into the structure such that it never falls out. That’s not a cache IMO.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Well, unless you consider the delay waiting for the device to get back on the Internet to just be a performance issue (-: However, even that’s not really correct because you could imagine someone using offline mode for an app on a device that’s never going to get back on to the Internet.

NSURLRequest.CachePolicy
 
 
Q