title: Swift iOS Cache WKWebView content for offline view
body: <p>We're trying to save the content (HTML) of WKWebView in a persistent storage (NSUserDefaults, CoreData or disk file). The user can see the same content when he re-enters the application with no internet connection. WKWebView doesn't use NSURLProtocol like UIWebView (see post <a href="https://www.raywenderlich.com/76735/using-nsurlprotocol-swift">here</a>).</p>
  
  <p>Although I have seen posts that "The offline application cache is not enabled in WKWebView." (Apple dev forums), I know that a solution exists.</p>
  
  <p>I've learned of two possibilities, but I couldn't make them work:</p>
  
  <p>1) If I open a website in Safari for Mac and select File >> Save As, it will appear the following option in the image below. For Mac apps exists  [[[webView mainFrame] dataSource] webArchive], but on UIWebView or WKWebView there is no such API. But if I load a .webarchive file in Xcode on WKWebView (like the one I obtained from Mac Safari), then the content is displayed correctly (html, external images, video previews) if there is no internet connection. The .webarchive file is actually a plist (property list). I tried to use a mac framework that creates a .webarchive file, but it was incomplete.</p>
  
  <p><a href="https://i.stack.imgur.com/gUu7m.png"><img src="https://i.stack.imgur.com/gUu7m.png" alt="enter image description here"></a></p>
  
  <p>2) I obtanined the HTML in webView:didFinishNavigation but it doesn't save external images, css, javascript</p>
  
  <pre><code> func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
  
      webView.evaluateJavaScript("document.documentElement.outerHTML.toString()",
          completionHandler: { (html: AnyObject?, error: NSError?) in
              print(html)
      })
  }
  </code></pre>
  
  <p>We're struggling over a week and it is a main feature for us.
  Any idea is really appreciated.</p>
  
  <p>Thank you!</p>