WKWebView does not open SOME target = "_ blank" links

I implemented the decisions made here and it works for some websites. For example, go to www.tomcruise.com and click on its trailers. Each of these links has target = "_ blank" and begins to open after implementing the solution proposed in the previously associated overflow column.

But now I found that if we go to here and click on any link (the one I tried starting with this question has an href tag as shown 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 WKWebView, the WKUIDelegate method below calls, but has navigationAction.request = ", and therefore nothing happens when the webView.loadRequest (" ") request is called. Does anyone else encounter this problem?

  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 especially important for the href tag above that calls the WKUIDelegate method with an empty URL?
  • How do we fix this problem? Let me know how you caused the root problem, as I'm also interested in debugging.
+2
html ios wkwebview
Aug 12 '15 at 4:10
source share
1 answer

I was hoping I could solve it using the delegate methods of WKWebView, but I could not understand.

So, I went to the UIWebView solution to run the javascript function after the web page has finished 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 an issue where clicking on links on any google plus Posts page resulted in a blank page loading.




UPDATE November 3, 2015. The phenomenon explained in the question no longer occurs for me in Swift 2.0 code. Therefore, you should be able to use the solution presented here for all your purposes.

+1
Aug 12 '15 at 22:35
source share



All Articles