我目前正在通过中的应用打开链接WebView
,但我正在寻找在Safari中打开链接的选项。
我目前正在通过中的应用打开链接WebView
,但我正在寻找在Safari中打开链接的选项。
Answers:
它不是“嵌入到Swift中”,但是您可以使用标准UIKit
方法来完成。看一下UIApplication的(已弃用)和。openUrl(_:)
open(_:options:completionHandler:)
Swift 4 + Swift 5(iOS 10及更高版本)
guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.shared.open(url)
Swift 3(iOS 9及更低版本)
guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.shared.openURL(url)
斯威夫特2.2
guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.sharedApplication().openURL(url)
iOS 9及更高版本中的新功能,您可以向用户显示SFSafariViewController
(请参见此处的文档)。基本上,您可以获得将用户发送到Safari而无需离开他们的应用程序的所有好处。要仅使用新的SFSafariViewController:
import SafariServices
在事件处理程序中的某个位置,向用户展示野生动物园视图控制器,如下所示:
let svc = SFSafariViewController(url: url)
present(svc, animated: true, completion: nil)
野生动物园视图将如下所示:
sharedApplication
禁止访问应用扩展中的属性。欲了解更多:developer.apple.com/library/archive/documentation/General/...
Swift 4更新:(提供给Marco Weber)
if let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") {
UIApplication.shared.openURL(requestUrl as URL)
}
或者使用以下更快速的样式guard
:
guard let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") else {
return
}
UIApplication.shared.openURL(requestUrl as URL)
斯威夫特3:
您可以通过以下方式隐式检查NSURL是否为可选:
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和iOS 10.2
UIApplication.shared.open(URL(string: "http://www.stackoverflow.com")!, options: [:], completionHandler: nil)
Swift 3和iOS 10.2
从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)
}
斯威夫特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)
}
在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迅捷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)
}
}