Fab*_*gue 125 safari webview swift
我目前正在我的应用程序中打开链接WebView,但我正在寻找一个选项来在Safari中打开链接.
Mik*_*e S 297
它并非"融入Swift",但您可以使用标准UIKit方法来实现它.看看UIApplication的openUrl().
斯威夫特4
guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.shared.open(url)
Run Code Online (Sandbox Code Playgroud)
斯威夫特3
guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.shared.openURL(url)
Run Code Online (Sandbox Code Playgroud)
Swift 2.2
guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.sharedApplication().openURL(url)
Run Code Online (Sandbox Code Playgroud)
man*_*ito 61
使用iOS 9及更高版本的新功能,您可以向用户显示SFSafariViewController(请参阅此处的文档).基本上,您可以获得将用户发送到Safari而不会让他们离开您的应用程序的所有好处.要使用新的SFSafariViewController:
import SafariServices
Run Code Online (Sandbox Code Playgroud)
并且在事件处理程序中的某个位置向用户显示safari视图控制器,如下所示:
let svc = SFSafariViewController(url: url)
present(svc, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
safari视图将如下所示:
Cod*_*ide 19
更新为Swift 4 :(归功于Marco Weber)
if let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") {
UIApplication.shared.openURL(requestUrl as URL)
}
Run Code Online (Sandbox Code Playgroud)
或使用以下更快捷的风格guard:
guard let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") else {
return
}
UIApplication.shared.openURL(requestUrl as URL)
Run Code Online (Sandbox Code Playgroud)
斯威夫特3:
您可以通过以下方式隐式检查NSURL是否为可选:
if let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") {
UIApplication.sharedApplication().openURL(requestUrl)
}
Run Code Online (Sandbox Code Playgroud)
Kir*_*odi 15
斯威夫特 5
Swift 5:检查使用canOpneURL是否有效然后它是打开的。
guard let url = URL(string: "https://iosdevcenters.blogspot.com/") else {
return
}
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
Run Code Online (Sandbox Code Playgroud)
ram*_*a n 12
Swift 3和IOS 10.2
UIApplication.shared.open(URL(string: "http://www.stackoverflow.com")!, options: [:], completionHandler: nil)
Run Code Online (Sandbox Code Playgroud)
Swift 3和IOS 10.2
Sam*_*ami 10
从iOS 10开始你应该使用:
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)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
85879 次 |
| 最近记录: |