WKWebview problems with opening ads or links


WKWebview problems with opening ads or links



I created a iOS webapp with WKWebview (Swift) for our website. Everything is going fine, but I have a lot of problems with clicking on ads and links. I handled it like this:


func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {

if navigationAction.navigationType == .linkActivated {

let url = navigationAction.request.url

if let url = navigationAction.request.url,
let host = url.host, !host.hasPrefix("www.example"),
UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
print(url)
print("Redirected to browser. No need to open it locally")
decisionHandler(.cancel)
} else if
url?.description.lowercased().range(of: "tel:") != nil
|| url?.description.lowercased().range(of: "mailto:") != nil
|| url?.description.lowercased().range(of: "whatsapp:") != nil
{
decisionHandler(.cancel)
print(url)
UIApplication.shared.openURL(url!)
} else {
print("Open it locally")
decisionHandler(.allow)
}
} else {
print("not a user click")
decisionHandler(.allow)
}
}



As you can see, it opens every url that do not belong to my website in safari, and I also handled it how to deal with share-buttons for calling, mailing or WhatsApp. But if I click on some ads, nothing happens (especially ads with javascript and maybe my website's name appears in the url of the ad). And my second problem is, that I have some other domains like this: "https://shop.example.com", but I want to open them inside my Webview.









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV