title: WKWebKit takeSnapshot(with snapshotConfiguration:) not working on iOS device
body: <p>Am I doing something wrong...?</p>
  
  <p>On iOS, I am wanting to take a snapshot of a full web page. Until iOS 11, this previously wasn't possible without dealing with a whole bunch of logic to scroll the <code>WKWebView</code>'s <code>scrollView</code>, etc.</p>
  
  <p>The following bug was reported to Webkit and fixed: <a href="https://bugs.webkit.org/show_bug.cgi?id=161450" rel="nofollow noreferrer">https://bugs.webkit.org/show_bug.cgi?id=161450</a></p>
  
  <p>Resulting in this change: <a href="https://trac.webkit.org/changeset/212929/webkit" rel="nofollow noreferrer">https://trac.webkit.org/changeset/212929/webkit</a></p>
  
  <p>Which now gives us a nice API on iOS 11 to take snapshot of <code>WKWebView</code>s content: <a href="https://developer.apple.com/documentation/webkit/wkwebview/2873260-takesnapshotwithconfiguration" rel="nofollow noreferrer">https://developer.apple.com/documentation/webkit/wkwebview/2873260-takesnapshotwithconfiguration</a></p>
  
  <p>This is all pretty great, except, I simply cannot get a snapshot when using this method on a device. When running the app in Simulator it work's great.</p>
  
  <p>I've added two images below for comparison. Both are the images that are returned with <code>takeSnapshotWithConfiguration:completionHandler:</code>, however one is blank for the most part.</p>
  
  <p>I've tried playing around with the web view's <code>frame</code>, the configuration, etc. but with no luck.</p>
  
  <p>I am thinking that perhaps Simulator links against the macOS version of WebKit and somehow it works fine there. One issue, as well is that the <code>WKSnapshotConfiguration</code> header also doesn't appear to be exposed in Swift, so I had to add a bridging header that imports it.</p>
  
  <p>Here's a sample project for anyone who's curious: <a href="https://github.com/runmad/WebKitSnapshotTest" rel="nofollow noreferrer">https://github.com/runmad/WebKitSnapshotTest</a></p>
  
  <p><a href="https://i.stack.imgur.com/LUZ01.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LUZ01.png" alt="screenshots"></a></p>
  
  <p><strong>UPDATE 10/20:</strong></p>
  
  <p>So this fixes it but is a nasty hack:</p>
  
  <pre><code>func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
      // webView.scrollView.contentSize == .zero initially, so put in a delay.
      // It still may be .zero, though. In that case we'll just call the
      // delegate method again.
      let deadlineTime = DispatchTime.now() + .milliseconds(300)
      DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
          guard webView.scrollView.contentSize.height &gt; 0 else {
              self.webView(webView, didFinish: navigation)
              return
          }
          // So this works... If you set the .frame of the webView
          // to that of the just loaded .scrollView and then load the
          // same URL again, the resulting .frame and .scrollView will
          // be the same size and the image render on a device will be
          // the correct size.
          guard self.hasLoadedOnce else {
              webView.frame = CGRect(x: self.view.bounds.width, y: 0, width: self.view.bounds.width, height: webView.scrollView.contentSize.height)
              webView.load(URLRequest(url: URL(string: "https://www.apple.com")!))
              self.hasLoadedOnce = true
              return
          }
          webView.snapshotWebView(completionHandler: { (image, _) in
              self.updateWith(image)
          })
      }
  }
  </code></pre>
  
  <p>So couple of issues:</p>
  
  <ol>
  <li><p>(<code>asyncAfter</code> I knew of this one already and had that in there) Have to add a delay for the snapshot, since the <code>scrollView</code> still might be <code>.zero</code> for a little bit</p></li>
  <li><p>Setting the size of the frame of the <code>WKWebView</code> to that of the correct <code>scrollView</code> size wasn’t working</p></li>
  <li><p>Setting the frame and THEN RELOADING the <code>WKWebView</code> in what will be it’s true size works</p></li>
  </ol>
  
  <p><strong>UPDATE 03/11:</strong></p>
  
  <p>My radar (35094298) was duped today to <a href="http://www.openradar.me/33812691" rel="nofollow noreferrer">33812691</a> and closed, which means Apple is aware of the issue, at least.</p>