0

I have a WKWebView inside my app. The problem is that when for example going on https://stockx.com/de-de and the user has the StockX app, the user is redirected to the app. Is there any way to disable this redirection in genreal for my webView? I couldn't find anything on this... Not too sure if you need code but here is my webView:

lazy var webView: WKWebView = {
    let webConfiguration = WKWebViewConfiguration()
    let webView = WKWebView(frame: .zero, configuration: webConfiguration)
    webView.uiDelegate = self
    webView.translatesAutoresizingMaskIntoConstraints = false
    return webView
}()

Apparently there is this function for an older Swift version:

func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: ((WKNavigationActionPolicy) -> Void)) {
    print("webView:\(webView) decidePolicyForNavigationAction:\(navigationAction) decisionHandler:\(decisionHandler)")

    let app = UIApplication.sharedApplication()
    let url = navigationAction.request.URL
    let myScheme: NSString = "https"
    if (url!.scheme == myScheme) && app.canOpenURL(url!) {
        print("redirect detected..")
        // intercepting redirect, do whatever you want
        app.openURL(url!) // open the original url
        decisionHandler(.Cancel)
        return
    }

    decisionHandler(.Allow)
}

But this is no longer working in Swift 5. Any idea how to to this in Swift 5?

Chris
  • 1,828
  • 6
  • 40
  • 108

1 Answers1

-1

So what is happening here it's web app initializing universal link. What you can theoretically do to block this:

Reference to this answer


func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: ((WKNavigationActionPolicy) -> Void)) {
        print("webView:\(webView) decidePolicyForNavigationAction:\(navigationAction) decisionHandler:\(decisionHandler)")

        let app = UIApplication.sharedApplication()
        let url = navigationAction.request.URL
        let myScheme: NSString = "https"
        if (url!.scheme == myScheme) && app.canOpenURL(url!) {
            print("redirect detected..")
            // intercepting redirect, do whatever you want
            app.openURL(url!) // open the original url
            decisionHandler(.Cancel)
            return
        }

        decisionHandler(.Allow)
    }

n3dx
  • 109
  • 6