它不是“融入 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
(请参阅文档 here)。基本上,您可以获得将用户发送到 Safari 的所有好处,而无需让他们离开您的应用程序。要使用新的 SFSafariViewController,只需:
import SafariServices
并且在事件处理程序的某处向用户呈现 Safari 视图控制器,如下所示:
let svc = SFSafariViewController(url: url)
present(svc, animated: true, completion: nil)
Safari 视图将如下所示:
https://i.stack.imgur.com/6pCEl.png
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
使用更多的 swift 风格:
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) }
斯威夫特 5
Swift 5: Check using canOpneURL
if valid then it's open.
guard let url = URL(string: "https://iosdevcenters.blogspot.com/") else {
return
}
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
LAUNCH: Launch failure with -10652/ <FSNode 0x10d50cf90> { isDir = y, path = '/Applications/Safari.app' }
斯威夫特 3 和 IOS 10.2
UIApplication.shared.open(URL(string: "http://www.stackoverflow.com")!, options: [:], completionHandler: nil)
斯威夫特 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)
}
在 Swift 1.2 中:
@IBAction func openLink {
let pth = "http://www.google.com"
if let url = NSURL(string: pth){
UIApplication.sharedApplication().openURL(url)
}
在 Swift 2.0 中:
UIApplication.sharedApplication().openURL(NSURL(string: "http://stackoverflow.com")!)
如果您使用 SwiftUI:
Link("Stack Overflow", destination: URL(string: "https://www.stackoverflow.com/")!)
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)
}
}
斯威夫特 5
if let url = URL(string: "https://www.google.com") {
UIApplication.shared.open(url)
}
不定期副业成功案例分享