2

I'm trying to watch youtube videos in my app's UIWebView itself. Whenever I tap on any of the webpage youtube link, it will automatically redirect into youtube application but youtube's app is also installed in my device. How can I block opening of one app from another app programmatically.

user28
  • 89
  • 5
Ravindhiran
  • 5,304
  • 9
  • 50
  • 82

1 Answers1

4

If you are using UIWebview

Add following delegate method in your viewcontroller. And assign webview.delegate = self

extension YourVC:UIWebViewDelegate{
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        if navigationType == UIWebViewNavigationType.linkClicked {
            return false
        }
        return true
    }
}

If you are using WKWebview,

Add following delegate method in your viewcontroller. And assign webView.navigationDelegate = self

extension YourVC:WKNavigationDelegate{
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if navigationAction.navigationType == .linkActivated {
            decisionHandler(.cancel);
            return;
        }
        decisionHandler(.allow);
    }
}
Mehul Thakkar
  • 12,440
  • 10
  • 52
  • 81