0

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

  • `controller.view.addSubview(web.view)` first you add that controller in your controller ? then want to present ... May i know the logic behind this ? – Jawad Ali May 08 '20 at 18:53
  • Is `safariVC` ever set? – Daniel Storm May 08 '20 at 18:53
  • Actually it had no sense for me also but if I remove the lines likes `web.view.topAnchor.constraint(...).isActive = true` I got a blank screen and if I add those lines it works only `controller.view.addSubview(web.view)` `SafariVC` is set only in the code at `safariVC = try authorizer...` – Camille Gallet May 08 '20 at 19:20
  • If I comment `controller.addChild(web)` and uncomment `controller.present(web, animated: true)` (https://pastebin.com/RQ5PVq9P) I have a window with title "Site name" – Camille Gallet May 08 '20 at 20:40

1 Answers1

1

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()
    }
}