Created
June 25, 2017 16:08
-
-
Save cristinaITdeveloper/cd7dda6c7e0c0d0427c51a0da4b217aa to your computer and use it in GitHub Desktop.
Swift - Open link in safari from web view - iOS (use this method in viewcontroller that implement the UIWebViewDelegate protocol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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