Since iOS 11, when the device is offline, network requests are most often fail quickly with a “The Internet connection appears to be offline.” error message (code -1009). Previously – as was the case for iOS version < 11 – request would first try and go through (parts of) the URL loading system.
To serve content to UIWebViews
while offline, I rely on a custom subclass of NSURLCache
, that returns cached data first, and requests the actual resource from the server afterwards in the background (only if needed/updated), to store for the next request.
I would like to force my app – particular my UIWebView
s I’d like to work offline – to still go through the URL loading system, so that my custom NSURLCache
is asked for a cached response every time (as was the case before iOS 11). Is this possible?
More background
While the request attempt fails immediately on iOS 11 when the device doesn’t have an internet connection, the OS also introduced the waitsForConnectivity
-flag to NSURLSession
. This does exactly what I want. However, this only works for my simpler requests that load a single file and that I was able to easily port from a NSURLConnection
-request to a NSURLSession
-request.
UIWebView
still uses NSURLConnection, and so I can’t use NSURLSession
and waitsForConnectivity
. But: is there another way to force going through the URL loading system and hit my custom NSURLCache
implementation?
In my web views, I simply request pages like this:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@“some/url.html" relativeToURL:[NSURL URLWithString:@“https://www.example.com/"]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
[self.webView loadRequest:request];
So, the above used to work and still works for iOS versions < 11. No luck with iOS 11.
I tried several values for the cachePolicy
parameter, including NSURLRequestReturnCacheDataDontLoad
. Still no luck.
And it’s not as simple as loading/requesting a single HTML file. The pages have lots of resources: CSS files, JS, images etc. And this was all transparently cached by my custom NSURLCache
. Sadly no more with iOS 11.
(Now: I’m using a custom subclass of NSURLCache
in the first place, because I couldn’t rely on the standard NSURLCache
implementation, no matter what configuration and cachePolicy
settings. If anyone has a suggestion for a different solution (without a custom NSURLCache
) to my problem, I’m definitely listening.)