1
votes

I'm using WKWebView in an application of mine. I've discovered that a variety of links are not clickable, even when I allow arbitrary loads through app transport security. For example, clicking all Adsense content and ads from other providers has no effect. The WebView doesn't even return any progress.

Additionally, trying to click on iTunes links (i.e an App Store badge) will not redirect to the App Store application. The WebView will register progress, however it will not leave the initial page.

Another example: clicking the badge on the Pinterest hompage that prompts you to open certain pins in the Pinterest app will only load a bnc.it URL and display a blank page, which is what I assume to be an intermediary between Pinterest.com and the Pinterest iPhone application.

If anyone knows how to get WKWebView to handle all of these such requests I'd greatly appreciate hearing how to do so.

1
Sorry to bump this but I'm just wondering if anyone has any ideasAlex Wulff

1 Answers

3
votes

Okay. I have figured how it works. The issue is about the navigationType. I have used the following delegate to make that work. I have been tested with my apps and it works probably. Only the Adsense ads will open Safari.

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        let requestedURL = navigationAction.request.url

        if navigationAction.navigationType == .linkActivated{
            print("You have clicked the requested URL \(requestedURL)")
            if requestedURL?.absoluteString.contains("doubleclick.net") == true{
                UIApplication.shared.open(requestedURL!, options: [:], completionHandler: nil)
                decisionHandler(.cancel)
            }
        }
        decisionHandler(.allow)
    }

Of course, you need to make sure you have declared

webView.uiDelegate = self
webView.navigationDelegate = self

I've also put the full simplified source code at my Github. Feel free to download it at the following address if you don't know what I mean:

https://github.com/babyghost666/WKWebViewAdsense

Feel free to replace,

UIApplication.shared.open(requestedURL!, options: [:], completionHandler: nil)

if you want the WKWebView do something else after the user clicked the Adsense. If this answer helps. Please vote and accept. Thanks.