J'ouvre actuellement le lien dans mon application dans un WebView, mais je recherche une option pour ouvrir le lien dans Safari à la place.
J'ouvre actuellement le lien dans mon application dans un WebView, mais je recherche une option pour ouvrir le lien dans Safari à la place.
Réponses:
Ce n'est pas "intégré à Swift", mais vous pouvez utiliser des UIKitméthodes standard pour le faire. Jetez un œil à UIApplication (obsolète) et .openUrl(_:) open(_:options:completionHandler:)
Swift 4 + Swift 5 (iOS 10 et supérieur)
guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.shared.open(url)
Swift 3 (iOS 9 et inférieur)
guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.shared.openURL(url)
Swift 2.2
guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.sharedApplication().openURL(url)
Nouveau avec iOS 9 et supérieur, vous pouvez présenter à l'utilisateur un SFSafariViewController(voir la documentation ici ). Fondamentalement, vous bénéficiez de tous les avantages d'envoyer l'utilisateur vers Safari sans le faire quitter votre application. Pour utiliser simplement le nouveau SFSafariViewController:
import SafariServices
et quelque part dans un gestionnaire d'événements, présentez à l'utilisateur le contrôleur de vue safari comme ceci:
let svc = SFSafariViewController(url: url)
present(svc, animated: true, completion: nil)
La vue safari ressemblera à ceci:
sharedApplicationpropriété dans l'extension d'application est interdit. Pour en savoir plus: developer.apple.com/library/archive/documentation/General/…
MISE À JOUR pour Swift 4: (crédit à Marco Weber)
if let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") {
UIApplication.shared.openURL(requestUrl as URL)
}
OU optez pour un style plus rapide en utilisant guard:
guard let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") else {
return
}
UIApplication.shared.openURL(requestUrl as URL)
Swift 3:
Vous pouvez vérifier NSURL comme facultatif implicitement en:
if let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") {
UIApplication.sharedApplication().openURL(requestUrl)
}
if let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") { UIApplication.shared.openURL(requestUrl as URL) }
Swift 3 et IOS 10.2
UIApplication.shared.open(URL(string: "http://www.stackoverflow.com")!, options: [:], completionHandler: nil)
Swift 3 et IOS 10.2
depuis iOS 10, vous devez utiliser:
guard let url = URL(string: linkUrlString) else {
return
}
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
Swift 5
Swift 5: Vérifiez en utilisant canOpneURLsi valide alors il est ouvert.
guard let url = URL(string: "https://iosdevcenters.blogspot.com/") else {
return
}
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
Dans Swift 1.2:
@IBAction func openLink {
let pth = "http://www.google.com"
if let url = NSURL(string: pth){
UIApplication.sharedApplication().openURL(url)
}
IOS 11.2 Swift 3.1 à 4
let webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
guard let url = URL(string: "https://www.google.com") else { return }
webView.frame = view.bounds
webView.navigationDelegate = self
webView.load(URLRequest(url: url))
webView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
view.addSubview(webView)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.navigationType == .linkActivated {
if let url = navigationAction.request.url,
let host = url.host, !host.hasPrefix("www.google.com"),
UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
print(url)
print("Redirected to browser. No need to open it locally")
decisionHandler(.cancel)
} else {
print("Open it locally")
decisionHandler(.allow)
}
} else {
print("not a user click")
decisionHandler(.allow)
}
}