SFSafariViewController是iOS开发中用于展示Safari浏览器的视图控制器。要实现自动关闭SFSafariViewController,可以使用以下方法:
SFSafariViewControllerDelegate
中的didFinish
方法。在该方法中,可以调用dismiss(animated:completion:)
方法来关闭SFSafariViewController。示例代码如下:
import SafariServices
class ViewController: UIViewController, SFSafariViewControllerDelegate {
// ...
func openSafariViewController() {
let url = URL(string: "https://example.com")!
let safariViewController = SFSafariViewController(url: url)
safariViewController.delegate = self
present(safariViewController, animated: true, completion: nil)
}
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
controller.dismiss(animated: true, completion: nil)
}
}
SFSafariViewControllerDidFinish
通知,并在通知回调中调用dismiss(animated:completion:)
方法来关闭SFSafariViewController。示例代码如下:
import SafariServices
class ViewController: UIViewController {
// ...
func openSafariViewController() {
let url = URL(string: "https://example.com")!
let safariViewController = SFSafariViewController(url: url)
NotificationCenter.default.addObserver(self, selector: #selector(dismissSafariViewController), name: NSNotification.Name.SFSafariViewControllerDidFinish, object: safariViewController)
present(safariViewController, animated: true, completion: nil)
}
@objc func dismissSafariViewController() {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.SFSafariViewControllerDidFinish, object: nil)
dismiss(animated: true, completion: nil)
}
}
这些方法可以根据具体需求选择使用。在实际开发中,可以根据业务逻辑和用户交互设计选择合适的方式来自动关闭SFSafariViewController。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云