如何关闭 swift 中的消息应用程序?

How to dismiss message app in swift?

你能在这里看到吗:https://vimeo.com/279403383

我正在尝试显示密码视图。因为此应用对安全敏感。

因此,如果应用确实进入了后台,则请求密码。

在这种情况下效果很好。

但是,具体案例工作很奇怪。

func applicationDidEnterBackground(_ application: UIApplication) {
            guard let passcodeManageView = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "passcodeManageView") as? PasscodeManageViewController else { return }
            passcodeManageView.state = State.loginMode
            passcodeManageView.modalPresentationStyle = .overFullScreen

            var rootViewController = UIApplication.shared.keyWindow?.rootViewController
            while let presentController = rootViewController?.presentedViewController {
                rootViewController = presentController
            }
            rootViewController?.present(passcodeManageView, animated: false, completion: nil)
    }

那么,我的问题是

  1. 密码视图如何覆盖MFMessageComposeViewController?

  2. 或者如何关闭 MFMessageComposeViewController?

最好的方法是什么???

您需要迭代应用中呈现的 ViewController 和 一一关闭。

func applicationDidEnterBackground(_ application: UIApplication) {  
    if let rootViewController = UIApplication.shared.keyWindow?.rootViewController {
      var  presented = rootViewController.presentedViewController
      while presented != nil
      {
        if presented is MFMessageComposeViewController {
          rootViewController.dismiss(animated: false, completion: nil)
          break
        } else {
           presented = presented?.presentedViewController
        }
      }

    }

    guard let passcodeManageView = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "passcodeManageView") as? PasscodeManageViewController else { return }
    passcodeManageView.state = State.loginMode
    passcodeManageView.modalPresentationStyle = .overFullScreen

    var rootViewController = UIApplication.shared.keyWindow?.rootViewController
    while let presentController = rootViewController?.presentedViewController {
      rootViewController = presentController
    }
    rootViewController?.present(passcodeManageView, animated: false, completion: nil)

}