wkwebview hyperlink not working in tableview

0





I am implementing wkwebview in my tableview cell. In each cellForRow At indexPath function, wkwebview is initialized and I load an HTML string in the webview.

here is my html string "

<html><header><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0'></header><body style="font-family: 'HelveticaNeue'; font-size: 14px; color: black"><ul><li>Online application on <a href='https://twitter.com/' class='link'>https://twitter.com/</a> </li><li>Lorem Ipsum is simply dummy text of the printing and typesetting </li><li><span data-mce-mark="1">Lorem Ipsum is simply dummy text of the printing and typesetting .</span></li></ul></body> </html>


"

Here I calculate the dynamic height of wkwebview and it working correctly

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) { if webView.tag < self.contentHeights.count{  // todo add function let currentContentHeight = webView.scrollView.contentSize.height if (self.contentHeights[webView.tag] < 50.0 && self.contentHeights[webView.tag] < currentContentHeight) { self.contentHeights[webView.tag] = webView.scrollView.contentSize.height webView.frame = CGRect(x: webView.frame.origin.x, y: webView.frame.origin.y, width: webView.frame.width, height: currentContentHeight) self.uiViewController?.reloadTableViewrow(atIndexPath: webView.tag) } } } }

But string loaded in wkwebview has hyperlinks in it, but on clicking on those hyperlinks,

Decide Policy for navigation function
is not called
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {  if navigationAction.navigationType == .linkActivated { let url = navigationAction.request.url if url?.description.lowercased().range(of: "http://") != nil || url?.description.lowercased().range(of: "https://") != nil || url?.description.lowercased().range(of: "//") != nil || navigationAction.targetFrame == nil{ decisionHandler(.cancel) UIApplication.shared.open(url!) } else { decisionHandler(.allow) } } else { decisionHandler(.allow) } }

Any reason why this is function is not getting called?

I checked if the delegate is attached or not but this function gets called the first time wkweview is initialised

I'm experiencing the same issue, and i'm disappointed that you haven't received a single reply or solution. Did you managed to find a solution on your own?

I'm experiencing the same issue. Not sure if it's same cause which this thread but hope to be able to help someone. 

My problem: I used below method for adding WKWebview to TableView. It worked for iOS 12. But it cannot work on iOS 14, 15 

[cellWeb addSubview:webView];

With this method, WKCompisitionComponent is not the top component in hierarchical controls. So webview cannot receive click event.

I changed to below way:

[cellWeb.contentView addSubview:webView]; 

And it worked on iOS 15.

wkwebview hyperlink not working in tableview
 
 
Q