【问题标题】:How to dismiss a SFSafariViewController in swift如何快速关闭 SFSafariViewController
【发布时间】:2020-05-08 18:50:56
【问题描述】:

我有一个函数可以实例化并显示一个显示登录页面的 SFSafariViewController

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
}

就是这样叫safariVC = try authorizer.authorizeSafariEmbedded(from: self,at: url!) 你会注意到我不使用 present 因为它会导致我的应用程序像那样崩溃 Application tried to present modally an active controller

登录完成后调用此函数,这里我想关闭 SFSafariViewController 所以我这样做

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

但它不起作用

【问题讨论】:

  • controller.view.addSubview(web.view) 首先你在你的控制器中添加那个控制器?然后想介绍...我可以知道这背后的逻辑吗?
  • safariVC 是否设置过?
  • 实际上它对我来说也没有任何意义,但如果我删除喜欢 web.view.topAnchor.constraint(...).isActive = true 的行,我会得到一个空白屏幕,如果我添加这些行,它只能工作 controller.view.addSubview(web.view) SafariVC 仅设置在代码safariVC = try authorizer...
  • 如果我评论 controller.addChild(web) 并取消评论 controller.present(web, animated: true) (pastebin.com/RQ5PVq9P) 我有一个标题为“站点名称”的窗口

标签: swift sfsafariviewcontroller


【解决方案1】:

我找到了解决办法

我只需要这样做:

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

【讨论】:

    猜你喜欢
    • 2018-08-10
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    相关资源
    最近更新 更多