如何关闭 UIView 上的 SFSafariViewController

How to dismiss SFSafariViewController on UIView

class CustomScreen: UIView, SFSafariViewControllerDelegate {
@IBAction func webPageBtnAction(_ sender: Any) {
    if #available(iOS 9.0, *) {
        let svc = SFSafariViewController(url: NSURL(string: "https://whosebug.com/")! as URL)
        svc.delegate = self
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.window?.rootViewController = svc
    } else {
        // Fallback on earlier versions
    }
}

@available(iOS 9.0, *)
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
    controller.dismiss(animated: false, completion: nil)
}
}

由于某些原因,我无法关闭 SFSafariViewController。有什么想法吗?

因为在您的代码中您要替换 rootViewController(对于这种情况来说是一种不好的做法),您应该简单地呈现 SFSafariViewController 这样做:

let svc = SFSafariViewController(url: NSURL(string: "https://whosebug.com/")! as URL)
svc.delegate = self
UIApplication.shared.keyWindow?.rootViewController?.present(svc, animated: true, completion: nil)

据我所知,这是一个不好的做法,因为 safariViewControllerDidFinish 永远不会被调用,因为 CustomScreen 将从当前 rootViewController[=16= 的替换中释放出来]