Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
605 views
in Technique[技术] by (71.8m points)

html - WKWebView not opening SOME target="_blank" links

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.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...