WKWebView没有打开某些目标="_ blank"链接 [英] WKWebView not opening SOME target="_blank" links

查看:111
本文介绍了WKWebView没有打开某些目标="_ blank"链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在此处实施了公认的解决方案.它确实适用于 一些网站.例如:访问www.tomcruise.com并单击他的预告片.这些链接中的每个链接都有target ="_ blank",并在实现先前链接的堆栈溢出文章中建议的解决方案后开始打开.

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.

但是现在我发现,如果我们转到此处,然后单击任意链接(一个在撰写此问题时,我尝试了如下所示的href标签

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>

当我从WKWebView内部单击此链接时,下面的WKUIDelegate方法确实被调用,但是具有navigationAction.request =",因此,在调用webView.loadRequest(")时没有任何反应.还有其他人遇到这个问题吗?

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
}

  • 上面指定的href标记导致WKUIDelegate方法以空网址调用的特殊之处是什么?
  • 我们如何解决此问题?让我知道您是怎么引起问题的,因为我也对调试感兴趣.
  • 推荐答案

    我希望我可以使用WKWebView委托方法来解决它,但我无法弄清楚.

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

    因此,我进入了UIWebView时代解决方案,即在完成网页加载后运行javascript函数

    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)
    }
    

    这解决了以下问题:点击任何google plus帖子页面中的链接会导致加载空白页面

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

    2015年11月3日更新:问题中解释的现象,在Swift 2.0代码中对我而言不再发生.因此,您应该能够在此处出于您的所有目的

    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

    这篇关于WKWebView没有打开某些目标="_ blank"链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆