如何在 Safari 中按下完成后执行代码?

How to execute code after pressing done in Safari?

我在应用程序中启动 Safari,并希望在我按“完成”后执行我的代码。但是函数func safariViewControllerDidFinish不会进入。

import SafariServices

class QRScannerController: UIViewController, WKNavigationDelegate, SFSafariViewControllerDelegate {

let vc = SFSafariViewController (url: URL(string: String(urls))!)
            present(vc, animated: true, completion: nil)
            vc.delegate = self
            
            func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
                controller.dismiss(animated: true) {
                 // do my code
                }

您不需要关闭 safariViewControllerDidFinish 中的控制器,点击完成时它已经关闭。

import SafariServices

class QRScannerController: UIViewController, SFSafariViewControllerDelegate {
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        let vc = SFSafariViewController (url: URL(string: "https://www.google.com")!)
        self.present(vc, animated: true, completion: nil)
        vc.delegate = self
    }

    func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
        // do your code
        print("done!")
    }
}