Skip to content

Instantly share code, notes, and snippets.

@cristinaITdeveloper
Created June 25, 2017 16:08
Swift - Open link in safari from web view - iOS (use this method in viewcontroller that implement the UIWebViewDelegate protocol
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
switch navigationType {
case .linkClicked:
// Open links in Safari
guard let url = request.url else { return true }
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
// openURL(_:) is deprecated in iOS 10+.
UIApplication.shared.openURL(url)
}
return false
default:
// Handle other navigation types...
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment