'How do I allow custom schemes to be executed while desisionHandler for external links was set?
I'm new to Swift coding. I'm trying to implement external links that should be opened in the user's default browser.
I quickly found a solution about it but this solution will also try opening the application magnet link in the external browser which is unnecessary because users need to be able to open the magnet links in my application.
Is there a way where I can also make the code allow running my specified magnet link locally?
This is what I've tried so far:
extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
// Check for links.
if navigationAction.targetFrame == nil {
// Make sure the URL is set.
guard let url = navigationAction.request.url else {
decisionHandler(.allow)
return
}
// Check for the scheme component.
let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
if components?.scheme == "magnet-link" {
decisionHandler(.allow)
} else if components?.scheme == "http" || components?.scheme == "https" {
// Open the link in the external browser.
NSWorkspace.shared.open(url)
// Cancel the decisionHandler because we managed the navigationAction.
decisionHandler(.cancel)
} else {
decisionHandler(.allow)
}
} else {
decisionHandler(.allow)
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
