Some redirect url links are not working when loaded by WKWebView

I'm trying to display one embedded content by using the WKWebView and I can not click some of these links. Is there anything I did wrong?
Here is the Link which I get the embedded content from:
https://publish.twitter.com
Here is the embedded content:
<a class="twitter-timeline" href="https://twitter.com/Google?ref_src=twsrc%5Etfw">Tweets by Google</a><script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>


Here is my code:

import UIKit
import WebKit

class ViewController: UIViewController {
    
    private var mWebView: WKWebView!
    let embeddedTwittContent = "<a class='twitter-timeline' href='https://twitter.com/Google?ref_src=twsrc%5Etfw'>Tweets by Google</a>"    //    let embeddedTwittContent = "<a class=\"twitter-timeline\" href=\"https://twitter.com/Smaforetagarna?ref_src=twsrc%5Etfw\">Tweets by Smaforetagarna</a>"
    let scriptValue = "<script async src=\"https://platform.twitter.com/widgets.js\" charset=\"utf-8\"></script>"
    let redirectLink = "https://twitter.com/Smaforetagarna/status/"


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        let preferences = WKPreferences()
        preferences.javaScriptEnabled = true
        
        let configuration = WKWebViewConfiguration()
        configuration.websiteDataStore =  WKWebsiteDataStore.nonPersistent()
        configuration.preferences = preferences
        
        self.mWebView = WKWebView(frame: CGRect.zero, configuration: configuration)
        
        self.mWebView.translatesAutoresizingMaskIntoConstraints = false
        self.mWebView.allowsLinkPreview = true
        self.mWebView.allowsBackForwardNavigationGestures = true
        self.mWebView.navigationDelegate = self
        self.view.addSubview(self.mWebView)
        
        // Add Constraints
        self.mWebView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 100).isActive = true
        self.mWebView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true
        self.mWebView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: 0).isActive = true
        self.mWebView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 0).isActive = true
        
        loadTweetNews()
    }
    
    func loadTweetNews(){
        let htmlHeader = "<!DOCTYPE html> <html><meta name=\'viewport\' content=\'initial-scale=1.0\'/> <head> \(scriptValue) </head> <body>"
        let htmlFooter = "</body> </html>"
        let orderHtml = htmlHeader + embeddedTwittContent + htmlFooter
        let url: URL = URL(string: "https:")!
        
        self.mWebView.loadHTMLString(orderHtml, baseURL: url)
    }


}

extension ViewController: WKNavigationDelegate{
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        print("page finished load")
    }
    
    func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
        print("didReceiveServerRedirectForProvisionalNavigation: \(navigation.debugDescription)")
    }
    
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        print("didStartProvisionalNavigation")
    }
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if navigationAction.navigationType == .linkActivated  {
            if let url = navigationAction.request.url,
                UIApplication.shared.canOpenURL(url) {
                UIApplication.shared.open(url)
                print(url)
                decisionHandler(.cancel)
            } else {
                decisionHandler(.allow)
            }
        } else {
            decisionHandler(.allow)
        }
    }
   

Hi,

I have the same issue and couldn't find any solution. I.e. I'm not able to get the navigation action when the heart icon of an embedded tweet was tapped.

Did you find a solution in the meantime?


Best

Sven


Edit:

Found a solution now. I just had to implement this method of WKUIDelegate:


func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
        if navigationAction.targetFrame == nil || navigationAction.targetFrame?.isMainFrame == false {
            if let urlToLoad = navigationAction.request.url {
                handleWebViewLink?(urlToLoad.absoluteString)// this is a closure, which is handled in another class. Nayway... here you get the url of "broken" links
            }
        }
        return nil
    }
@ Sven what is the trick here - I cant get the createWebViewWith to get called ... so annoying.

Code Block
public class WebViewStore: ObservableObject {
  @Published public var navDelegate = WebViewNavDelegate();
  @Published public var uiDelegate = WebViewUIDelegate();
 
  public init(webView: WKWebView = WKWebView()) {
    log.info("WebViewStore.init")
     
    self.webView.navigationDelegate = navDelegate;
    self.webView.uiDelegate = uiDelegate;
}

Some redirect url links are not working when loaded by WKWebView
 
 
Q