如何快速关闭 SFSafariViewController

Posted

技术标签:

【中文标题】如何快速关闭 SFSafariViewController【英文标题】:How to dismiss a SFSafariViewController in swift 【发布时间】: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) 我有一个标题为“站点名称”的窗口 【参考方案1】:

我找到了解决办法

我只需要这样做:

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

【讨论】:

以上是关于如何快速关闭 SFSafariViewController的主要内容,如果未能解决你的问题,请参考以下文章

如何快速关闭 SFSafariViewController

如何快速编写以下方法? [关闭]

您如何在中间位置实现枢轴快速排序,这种变化称为啥? [关闭]

如何快速制作快捷方式? [关闭]

如何在快速向上或向下滚动时退出键盘? [关闭]

如何快速打开和关闭手电筒?