How to cache resources loaded by WKWebView?

2019-07-24 18:17发布

问题:

I use WKWebView to display rich text including images.

I want to cache these images on disk.

How can I get these HTTP requests and cache the responses?

回答1:

Caching for the all resources loaded in WKWebView is set by default. All you have to do is to set the right headers in the HTTP response for these resources so that iOS SDK will cache these honoring the rules specified in the Cache-Control header in the Response.

For example you set the max-age to some big number in Cache-Control to ensure caching for longer durations.

Refer the below article which explains all parameters in the HTTP Response which the iOS SDK will use to implement caching on the device side.

https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching?hl=en