iosuiwebviewwkwebview3dtouch

How to open 3D touch pop pages in the same WKWebView when allowsLinkPreview enabled?


When set webView.allowsLinkPreview = true, links support 3D touch peek & pop feature. The only problem is the new page will open in Safari instead of the webView.


Solution

  • According to Apple iOS 10 introduced a new API for WKWebView allowing you to intercept the peek and pop actions of the link preview by using the following delegate methods:

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

    Just use the second delegate method to present the url in your own tab or load it in your own WKWebView.

    Source: iOS 10 Link Preview API in WKWebView