Search code examples
iosswiftuiwindow

How to dismiss message app in swift?


Can you see that here : https://vimeo.com/279403383

I'm trying present a passcode view. Because this app is sensitive to security.

So if app did enter background, request passcode.

In this case pretty well work.

But, specific case work oddly.

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

So, my question is

  1. How does passcode view cover MFMessageComposeViewController?

  2. or How to dismiss MFMessageComposeViewController?

What is the best way???


Solution

  • You need to iterate the presented ViewControllers of your app and close them one by one.

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