Camille Gallet
Camille Gallet

Reputation: 77

How to dismiss a SFSafariViewController in swift

I've a function which instantiate and show a SFSafariViewController which shows a login page

public func authorizeSafariEmbedded(from controller: UIViewController, at url: URL) throws -> SFSafariViewController {
    safariViewDelegate = OAuth2SFViewControllerDelegate(authorizer: self)
    let web = SFSafariViewController(url: url)
    web.title = oauth2.authConfig.ui.title
    web.delegate = safariViewDelegate as! OAuth2SFViewControllerDelegate
    controller.addChild(web)
    controller.view.addSubview(web.view)
    web.didMove(toParent: controller)
    web.view.translatesAutoresizingMaskIntoConstraints = false
    if #available(iOS 11.0, *) {
        web.view.topAnchor.constraint(equalTo: controller.view.safeAreaLayoutGuide.topAnchor, constant: 30).isActive = true
        web.view.bottomAnchor.constraint(equalTo: controller.view.safeAreaLayoutGuide.bottomAnchor, constant: -30).isActive = true
        web.view.leadingAnchor.constraint(equalTo: controller.view.safeAreaLayoutGuide.leadingAnchor, constant: 30).isActive = true
        web.view.trailingAnchor.constraint(equalTo: controller.view.safeAreaLayoutGuide.trailingAnchor, constant: -30).isActive = true
    } else {
        // Fallback on earlier versions
    }
    if #available(iOS 10.0, *), let barTint = oauth2.authConfig.ui.barTintColor {
        web.preferredBarTintColor = barTint
    }
    if #available(iOS 10.0, *), let tint = oauth2.authConfig.ui.controlTintColor {
        web.preferredControlTintColor = tint
    }
    web.modalPresentationStyle = .fullScreen

    willPresent(viewController: web, in: nil)
    //controller.present(web, animated: true)

    return web
}

It is called like that safariVC = try authorizer.authorizeSafariEmbedded(from: self,at: url!) You'll notice that I don't use present because it cause my app to crash like that Application tried to present modally an active controller

when the login is done this function is called, here I want to dismiss the SFSafariViewController so I do

func hideSafariView(){
    print ("sf \(safariVC)")
    if safariVC != nil {
        print("dissmis")
        safariVC!.dismiss(animated: true, completion: nil)
    }
}

But it don't works

Upvotes: 0

Views: 1565

Answers (1)

Camille Gallet
Camille Gallet

Reputation: 77

I've found a solution

I simply need to do that:

func hideSafariView(){
    if safariVC != nil {
        print("dissmis")
        safariVC?.removeFromParent()
        safariVC!.dismiss(animated: true, completion: nil)
        safariVC!.view.removeFromSuperview()
    }
}

Upvotes: 1

Related Questions