WKWebView Image Caching

I'm working on an app and my task is to cache assets from a webview (in this case, a shopping cart) to speed up subsequent loads. I've messed around with cachePolicy, but it looks like the same images are still being loaded every time. 

What can I do to cache assets from a webview?

Under the covers WKWebView uses Foundation networking (NSURLSession) for its networking. That in turn uses NSURLCache. NSURLCache implements standard web caching semantics. NSURLCache gives you very little control over what it chooses to cache, and even less so when used in by a WKWebView. Mostly this caching is automatic, driven by the various cache control headers returned by the server. IMO that’s the best place to look for caching improvements: Make sure that your server is returning images with a reasonable etag, TTL, and so on.

Share and Enjoy

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

Thanks for the feedback, Quinn!

Someone on our team brought up the possibility of using WKURLSchemeHandler to intercept the request and cache the assets. Is that a plausible solution (and how might that work), or is that not a viable path forward?

Thanks again!

Is that a plausible solution … ?

It depends on whether you control the HTML being loaded into your web page. Scheme URL handlers do not let you intercept HTTP[S] requests, so for this to work you have to make sure that your images are referenced with a custom URL scheme. If you can tweak your HTML to do that then, sure, a scheme URL handler will let you intercept the request and cache however you want.

Then again, if you have that level of control over the website being loaded, tweaking the cache parameters is likely to be a much easier path forward (-:

Share and Enjoy

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

WKWebView Image Caching
 
 
Q