I’m trying to exclude loading a specific domain in Safari (or any default browser in iOS) when tapped inside a WKWebView, without removing other conditions I already put in the function (as you can see below):
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if let url = navigationAction.request.url, ["mailto", "tel"].contains(url.scheme) {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
decisionHandler(.cancel)
}
} else {
decisionHandler(.allow)
}
}
I tried in different ways, but depending on the code I wrote all the links were loaded in Safari (even those that should have remained in the WKWebView) or none of the links worked anymore.
I’m also writing an app for Android with similar features, and I found this kind of syntax which allowed me to get the result I wanted, so I wondered if there’s something similar for iOS:
if (!url.contains("vivibusso.app") || url.startsWith("tel:") || url.startsWith("mailto:")) {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(intent)
return true
} else {
view.loadUrl(url)
}
return true
Can you suggest me the correct syntax to solve this problem?
2
Answers
I finally solved the issue, but without the advice that iShox gave me in his answer, I might never have gotten there...
This is the code that allowed me to get the desired result:
Just to explain a little bit how it works, basically the code checks if the page I'm visiting points to a specific domain, and if true open the page inside the WKWebView of the app; otherwise, it checks if there's an app on the device capable of opening that kind of link and passes it to the one set by default - and this applies not only to websites links but also to other types of links such as mailto: and tel: (which was another result I wanted to achieve).
Try this one