iOS WKWebView Peek and Pop on an Image

513 Views Asked by At

Lately I have been trying to get Peek And Pop to work with my WKWebView. The use case I use it for is a RichText comment section in my App. But because there is already done plenty of customization done to the WebView I setup a minimal reproducible example.

So far I found out the following:

  1. The property allowsLinkPreview of WKWebView should be true, which it defaults to.
  2. You should implement a custom WKUIDelegate in which:
    • webView(_ webView: WKWebView, shouldPreviewElement... should return true if you want to show a Peek and Pop.
    • webView(_ webView: WKWebView, previewingViewControllerForElement... should return the view controller to display for pop and peek.
    • webView(_ webView: WKWebView, commitPreviewingViewController... once the peek and pop is completed you should push the view to a navigation controller or present it modally here.

The effect I am trying to achieve is displayed here.

Problem: What currently goes wrong is that I do return true from shouldPreviewElement, but then the second method, previewViewControllerForElement is never invoked as expected. I have also looked at other Pop and Peek implementations (outside of WKWebView and they work similarly and do not have this problem).

0

There are 0 best solutions below