Is it possible to implement a custom behavior for 3D Touch on links with WKWebView?

463 views Asked by At

I'm trying to add my own implementation of 3D touch on links with WKWebView so I set the allowsLinkPreview property of my WKWebView to false.

But then, impossible to check whether the 3D touch was on a link and impossible to know what link neither.

So I'm wondering if there's a way to set the allowsLinkPreview property to true and then to catch the peek event that's triggered when 3D Touching a link?

1

There are 1 answers

0
Patrick On

It is possible. You should set the allowsLinkPreview value to true and then implement the following methods. Both methods need implemented or else it won't work.

optional func webView(_ webView: WKWebView,  previewingViewControllerForElement elementInfo: WKPreviewElementInfo,  defaultActions previewActions: [WKPreviewActionItem]) -> UIViewController?
optional func webView(_ webView: WKWebView,  commitPreviewingViewController previewingViewController: UIViewController)

In my case, I didn't want to create a previewViewController so I just returned an SFSafariViewController and passed in my url. That seemed to work perfectly. As for the commitPreviewingViewController method, that is where I put my code to open up a new tab in my browser.

Source: iOS 10 Link Preview API in WKWebView