我目前打开我的应用程序中的链接在一个 WebView,但我正在寻找一个选项,以打开链接在 旅行而不是。
WebView
它不是“嵌入到 Swift 中”,但是您可以使用标准的 UIKit方法来实现它。看一下 UIApplication 的 openUrl(_:)(弃用)和 open(_:options:completionHandler:)。
UIKit
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(iOS9及以下版本)
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)
更新为 Swift 4: (归功于 Marco Weber)
if let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") { UIApplication.shared.openURL(requestUrl as URL) }
或者使用 guard更加迅捷的风格:
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) }
在 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")!)
IOS9或更高版本的新版本可以为用户提供 SFSafariViewController(参见文档 给你)。基本上,你可以在不让用户离开你的应用程序的情况下把用户发送到 Safari 上,从而获得所有的好处。要使用新的 SFSafariViewController,只需:
SFSafariViewController
import SafariServices
并且在事件处理程序中的某个地方向用户显示 Safari 视图控制器,如下所示:
let svc = SFSafariViewController(url: url) present(svc, animated: true, completion: nil)
野生动物园的景观将会是这样的:
Swift 3 & IOS 10.2
UIApplication.shared.open(URL(string: "http://www.stackoverflow.com")!, options: [:], completionHandler: nil)
从 iOS10开始,你应该使用:
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) }
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) } }
Swift 5
雨燕5: 检查使用 canOpneURL如果有效,那么它是打开的。
canOpneURL
guard let url = URL(string: "https://iosdevcenters.blogspot.com/") else { return } if UIApplication.shared.canOpenURL(url) { UIApplication.shared.open(url, options: [:], completionHandler: nil) }
if let url = URL(string: "https://www.google.com") { UIApplication.shared.open(url) }
如果你使用 SwiftUI:
Link("Stack Overflow", destination: URL(string: "https://www.stackoverflow.com/")!)