Skip to content

Instantly share code, notes, and snippets.

@apple-avadhesh
Created November 18, 2021 18:39
Show Gist options
  • Save apple-avadhesh/3e670b28b8503b31d949246c058a3b53 to your computer and use it in GitHub Desktop.
Save apple-avadhesh/3e670b28b8503b31d949246c058a3b53 to your computer and use it in GitHub Desktop.
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
@IBOutlet weak var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.uiDelegate = self
webView.navigationDelegate = self
webView.allowsBackForwardNavigationGestures = true
let myURL = URL(string: [====URL====])
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: (WKNavigationActionPolicy) -> Void) {
guard let redirectURL = (navigationAction.request.url) else {
decisionHandler(.cancel)
return
}
if (redirectURL.absoluteString.contains("tel:") ) {
UIApplication.shared.open(redirectURL, options: [:], completionHandler: nil)
}
if (redirectURL.absoluteString.contains("whatsapp") ) {
UIApplication.shared.open(redirectURL, options: [:], completionHandler: nil)
}
decisionHandler(.allow)
}
}
extension ViewController: WKUIDelegate {
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
guard let url = navigationAction.request.url else {
return nil
}
guard let targetFrame = navigationAction.targetFrame, targetFrame.isMainFrame else {
webView.load(URLRequest(url: url))
return nil
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment