Search code examples
iosswiftuiviewcontrollersegue

PerformSegue after getting an email result


    When sending an email:


        if MFMailComposeViewController.canSendMail()
            {
                let mail = MFMailComposeViewController()
                mail.mailComposeDelegate = self
                // the set to receipient
                // is always this email
                // since this is the owners email
                mail.setSubject("ORDER CONFIRMATION RODEO'S CATERING")
                mail.setToRecipients(["rodeoscatering2018@gmail.com"])
                mail.setMessageBody( m_information_for_body , isHTML: false)
                self.present(mail, animated: true)
            }


         func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?)
         {
          // In here I am checking if it is sent
         case .sent:
            do
            {
               print("sent")

                controller.dismiss(animated: true, completion: nil)
                // and then doing a performSegue below...
                // performSegue(withIdentifier....)
            }

          }

I know it is not a problem with the identifier and all segues are there. I see the print message. But unfortunately all that happens, is that when the Email prompt shows up I press Send, then the email controller gets dismissed (which is good) and takes me to the current screen I am on and the print message shows, however the performSegue doesn't occur.

I essentially want it to where if the .sent case has occurred then go back to a home page

Swift 4.2 & Latest xCode 10.1


Solution

  • Credit to Willijay:

    to show code example of his comment that worked: This does the following: - If mail sent then we display an alert saying it was succesful then performSegue() to home screen when user presses 'Ok' on the alert:

    func go_back_home() -> Void
    {
        let title   = "Confirmation Order Status"
        let message = "Message was sent. Check email for a copy"
    
        let alert = UIAlertController(
            title: title,
            message: message,
            preferredStyle: .alert)
    
        alert.addAction(UIAlertAction(title:"OK", style: .default, handler:
        {
            action in self.performSegue(withIdentifier: "order_info_back_to_home_page", sender: self)
    
        }))
    
        self.present(alert, animated: true)
    }
    

    Then the way we make use of go_back_home() function is this:

    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?)
             {
              // In here I am checking if it is sent
             case .sent:
                do
                {
                   print("sent")
    
                    controller.dismiss(animated: true, completion: go_back_home)
                }
    
              }