4

I have implemented the accepted solutions here and it does work for some websites. For eg: Go to www.tomcruise.com and click on his trailers. Each of those links have target="_blank" and started opening after implementing the solution suggested in the previously linked stack overflow post.

But now I found that if we go to here and click on any link(the one I tried, as of writing this question, has a href tag as below

 <a rel="nofollow" target="_blank" href="http://www.nowmagazine.co.uk/celebrity-news/victoria-and-david-beckham-fighting-to-be-together-296082" class="ot-anchor aaTEdf" jslog="10929; track:click" dir="ltr">http://www.nowmagazine.co.uk/celebrity-news/victoria-and-david-beckham-fighting-to-be-together-296082</a>

When I click on this link from inside WKWebView, the WKUIDelegate method below, does get called but has navigationAction.request = "" and hence nothing happens when webView.loadRequest("") gets called. Anyone else face this issue?

  optional func webView(_ webView: WKWebView, createWebViewWithConfiguration configuration: WKWebViewConfiguration,
       forNavigationAction navigationAction: WKNavigationAction,
            windowFeatures windowFeatures: WKWindowFeatures) -> WKWebView?{
      if navigationAction.targetFrame == nil {
          webView.loadRequest(navigationAction.request)
      }

      return nil
}
  • What is special about the above specified href tag that is causing the WKUIDelegate method to be called with an empty url?
  • How do we fix this issue? Let me know how you root caused the issue as I am interested in debugging as well.
Community
  • 1
  • 1
shrutim
  • 1,058
  • 2
  • 12
  • 21
  • For me the solutions you used work. Also for the google plus page you linked to.. the now magazine link also works for me –  Aug 27 '15 at 14:04

1 Answers1

4

I was hoping that I could solve it using the WKWebView delegate methods, but I could not figure it out.

So I went to the UIWebView era's solution of running a javascript function upon completion of web Page loading

func webView(webView: WKWebView, didFinishNavigation navigation: WKNavigation!) {
    let jsCode = "var allLinks = document.getElementsByTagName('a');if (allLinks) { var i;for (i=0; i<allLinks.length; i++) {var link = allLinks[i];var target = link.getAttribute('target');if (target && target == '_blank') {link.setAttribute('target','_self');} } }"
    webView.evaluateJavaScript(jsCode, completionHandler: nil)
}

This fixed the issue where tapping on the links in any google plus Posts page was resulting in an empty page being loaded


UPDATE on Nov 3rd 2015: The phenomenon explained in the question, no longer happens for me in Swift 2.0 code. So , you should be able to use the solution presented here for all your purposes

Community
  • 1
  • 1
shrutim
  • 1,058
  • 2
  • 12
  • 21
  • I have similar issue but your solution doesn't work for me, I am new to JS, can you please look into issue I am facing : – Tarun Tanwar Jan 23 '20 at 09:22
  • Thanks - Swift 5.3, ios14, 2020 - this code still work. The stupid WKWebView is broken again. – Deian Oct 20 '20 at 03:45