0

I have a problem with opening links with target="_blank" which are in the iframe in WKWebView. Normally, these kind of links are opened in new windows, but WKWebView blocks it.

In my situation, I have to load HTML from string with embedded script of 3rd party library, which onLoad inject iframe in body of view (so I can't change anything in this code). The HTML after load looks similar to this:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        ... Script which injects on load
    </head>
    <body>
        <iframe sandbox="allow-scripts allow-same-origin" ...>
            <a href="some url" target="_blank">LINK</a>
        </iframe>
    </body>
</html>

The finish effect which I'd like to achieve is to open this links in Safari, outside of the app.

Solutions which I tried:

  • add uiDelegate to WKWebView and listen to events in func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? which was proposed here (it is called on loading a page so it is correctly configured)
  • add <base target="_parent"> or <base target="_top"> to <head> tag which was proposed here
  • add event listeners to all <a> tags in document which was proposed here
  • Does this answer your question? [Why is WKWebView not opening links with target="\_blank"?](https://stackoverflow.com/questions/25713069/why-is-wkwebview-not-opening-links-with-target-blank) – timbre timbre Nov 18 '22 at 14:46

0 Answers0