在 iOS Safari 中打开链接
在 Swift 中,有两种打开链接的方法。一种方法是使用 UIApplication 类在 Safari 浏览器中打开链接。另一种方法是使用 iOS 中的 SafariServices 框架。让我们看一些基于这两种方法打开链接的示例。
方法 1:使用 UIApplication 类
步骤 1 − 为要打开的链接创建一个 URL 对象。您可以使用 URL(string:) 初始化器来执行此操作。
步骤 2 − 创建 UIApplication 类的实例。此类负责管理应用的行为,包括打开 URL。
步骤 3 − 使用 UIApplication 类的 open(_:options:completionHandler:) 方法打开 URL。此方法接受三个参数:要打开的 URL、选项字典和完成处理程序。
您可以使用选项字典提供其他打开 URL 的选项,包括在前台还是后台打开,以及是否在无法打开时显示警报。在本例中,我们通过传递一个空字典来使用默认设置。
打开 URL 后(或发生错误时),将调用一个名为完成处理程序的闭包。在本例中,我们通过传递 nil 来使用默认完成处理程序。
示例
综上所述,以下是使用 UIApplication 类打开链接的示例 −
import UIKit
class TestController: UIViewController {
private lazy var clickButton: UIButton = {
let button = UIButton()
button.addTarget(self, action: #selector(handleButtonClick), for: .touchUpInside)
button.backgroundColor = .darkGray
button.setTitle("Click to open link", for: .normal)
button.setTitleColor(.white, for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .semibold)
button.layer.cornerRadius = 5
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
initialSetup()
}
private func initialSetup() {
view.backgroundColor = .white
navigationItem.title = "Open Link"
view.addSubview(clickButton)
clickButton.widthAnchor.constraint(equalToConstant: 200).isActive = true
clickButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
clickButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
clickButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
}
@objc private func handleButtonClick() {
if let url = URL(string: "https://www.tutorialspoint.com") {
let application = UIApplication.shared
application.open(url, options: [:], completionHandler: nil)
}
}
}
输出

在上面的示例中,我们使用字符串创建了一个 URL 对象。然后,我们使用共享 UIApplication 实例的 open 方法打开该 URL。
这将在设备上的默认浏览器应用中打开该 URL,通常是 iOS 上的 Safari。请注意,此方法会将用户从您的应用切换到浏览器应用。
方法 2:使用 SafariServices 框架
步骤 1 − 首先,将 SafariServices 框架导入您的 Swift 文件中。
步骤 2 − 为要打开的链接创建一个 URL 对象。您可以使用 URL(string:) 初始化程序执行此操作。
步骤 3 − 接下来,创建一个 SFSafariViewController 实例并传入要打开的 URL。
步骤 4 − 使用视图控制器的 present(_:animated:completion:) 方法呈现 SFSafariViewController。
SFSafariViewController 将负责显示网页并允许用户与之交互。当用户完成浏览后,他们只需关闭 SFSafariViewController 即可返回到您的应用。
示例
综上所述,以下是使用 SafariServices 框架打开链接的示例 −
import UIKit
import SafariServices
class TestController: UIViewController {
private lazy var clickButton: UIButton = {
let button = UIButton()
button.addTarget(self, action: #selector(handleButtonClick), for: .touchUpInside)
button.backgroundColor = .darkGray
button.setTitle("Click to open link", for: .normal)
button.setTitleColor(.white, for: .normal)
button.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .semibold)
button.layer.cornerRadius = 5
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
initialSetup()
}
private func initialSetup() {
view.backgroundColor = .white
navigationItem.title = "Open Link"
view.addSubview(clickButton)
clickButton.widthAnchor.constraint(equalToConstant: 200).isActive = true
clickButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
clickButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
clickButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
}
@objc private func handleButtonClick() {
if let url = URL(string: "https://www.tutorialspoint.com") {
let safariViewController = SFSafariViewController(url: url)
present(safariViewController, animated: true, completion: nil)
}
}
}
输出

这将在您的应用内的 Safari 浏览器视图控制器中打开 URL,为您的用户提供无缝的浏览体验。SafariServices 框架还为浏览器视图控制器提供了多种自定义选项,允许您定制浏览器的外观和行为,以匹配您应用的设计。
结论
总而言之,使用 SafariServices 框架在 Swift 中访问链接是一种简单有效的方法,可以为您的用户提供完美的浏览体验。该框架的 SFSafariViewController 类可让您轻松地在应用内显示在线信息,同时保持统一的外观和风格。
此外,SafariServices 框架还为浏览器视图控制器提供了多种自定义选项,让您能够修改浏览器的外观和风格,使其与应用的设计相得益彰。虽然这会将用户从您的应用移至设备的默认浏览器应用,但如果您需要在应用外打开链接,您也可以使用 UIApplication 类的 open(_:options:completionHandler:) 方法。