2

I need to cache webview url on WKWebview. I'm able to do it using the below configuration

var webViewConfiguration:WKWebViewConfiguration {
    get {
      // Create WKWebViewConfiguration instance
      let webCfg:WKWebViewConfiguration = WKWebViewConfiguration()
      // Configure the WKWebViewConfiguration instance with the WKUserContentController
      webCfg.userContentController = userContentController
      webCfg.websiteDataStore = WKWebsiteDataStore.default()
      webCfg.processPool = ProcessPool.shared.processPool
      return webCfg
    }
  }

and while loading the webview I'm using the below code:

let request = URLRequest(url: self.url!, cachePolicy: .returnCacheDataElseLoad, timeoutInterval: 600)
        self.webView.load(request)

Issue I'm facing right now is the cache is taking time on every launch. i.e on every launch webview is taking a lot of time to load, after one load it is loading fast.

What I need to achieve is once webview is loaded, it should load faster on consecutive loads.

Rufat Mirza
  • 1,425
  • 14
  • 20

2 Answers2

0

.useProtocolCachePolicy is the default policy for URL load requests. It should work for your case.

let request = URLRequest(url: self.url!, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 600)
Rufat Mirza
  • 1,425
  • 14
  • 20
0

Using the default .useProtocolCachePolicy instead of .returnCacheDataElseLoad is OK. 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. On the server side, set the Cache-Control HTTP headers max age for its responses.

Q.u.a.n.g L.
  • 1,564
  • 1
  • 12
  • 27