How to execute code after pressing done in Safari?

314 Views Asked by At

I start Safari in an application and want to execute my code after I press Done. But the function func safariViewControllerDidFinish won't go into.

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
                }
1

There are 1 best solutions below

0
On BEST ANSWER

You don't need to dismiss the controller inside safariViewControllerDidFinish, it is already dismissed when done is tapped.

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!")
    }
}