class CustomScreen: UIView, SFSafariViewControllerDelegate {
@IBAction func webPageBtnAction(_ sender: Any) {
if #available(iOS 9.0, *) {
let svc = SFSafariViewController(url: NSURL(string: "https://stackoverflow.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://stackoverflow.com/")! as URL)
svc.delegate = self
UIApplication.shared.keyWindow?.rootViewController?.present(svc, animated: true, completion: nil)
据我所知,这是一个不好的做法,因为safariViewControllerDidFinish
永远不会被调用,因为CustomScreen
将被取代分配当前的rootViewController