Your browser preventing this app to open “URL”

Hi Apple developers, I am very new to XCode and Swift, I am planning to build an app for iOS from a web. I tried to use WKWebView to handle the web , I managed to redirect some of the links to Safari, however some button/links didn't trigger .linkActivated function and encounter the error as "Your browser preventing this app to open “URL”. If I copy the URL to Safari is able to open, I trying to research on web but can't find any related solution for my case.

Here is the code in my app:

import UIKit import WebKit import SafariServices

class ViewController: UIViewController, WKNavigationDelegate {

var webView: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()
    
    // Initialize WKWebView
    let webConfiguration = WKWebViewConfiguration()
    //enable javascript
    webConfiguration.preferences.javaScriptEnabled = true
    webView = WKWebView(frame: self.view.frame, configuration: webConfiguration)
    webView.navigationDelegate = self
    self.view.addSubview(webView)
    
    // Load a web page as webview
    if let url = URL(string: "https://myurl") {
        let request = URLRequest(url: url)
        webView.load(request)
    }
    //console log
    webView.evaluateJavaScript("console.log('Button clicked!')") { result, error in
        if let error = error {
            print("Error executing JavaScript: \(error.localizedDescription)")
        } else {
            print("JavaScript result: \(String(describing: result))")
        }
    }
}   



func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    
    if let url = navigationAction.request.url, navigationAction.navigationType == .linkActivated {
        // Check if URL is external and open it in Safari
        if UIApplication.shared.canOpenURL(url) {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
            decisionHandler(.cancel) // Prevent loading the link in the WebView
        } else {
            decisionHandler(.allow) // Allow loading if URL cannot be opened in Safari
        }
    } else {
        decisionHandler(.allow) // Allow the WebView to load the URL normally
    }
}    

}

It seems the same issue that here when handling the Universal Link over javascript. https://forums.developer.apple.com/forums/thread/772550

Could you kindly provide a simple project that demonstrates the issue we are experiencing? Alternatively, could you share a published website? Please include the links to the AASA file so that we can verify the links that should open the application. Once you open the bug report, please post the FB number here for my reference.

If you have any questions about filing a bug report, take a look at Bug Reporting: How and Why?

Albert Pascual
  Worldwide Developer Relations.

Your browser preventing this app to open “URL”
 
 
Q