swiftxcodemfmailcomposeviewcontroller

How do I get my app to print ''E-Mail send'' or ''E-Mail cancelled"?


I have an app which allows me to send E-Mails. I already got it as far, as that the Mail Controller is closed after type on the send or cancel button. But I would like to have an ''Popup'' or something inside of the app which tell the user whether they have send the mail or cancelled it.

The commented out code had been my try let the app say it, but it wouldn't work because of some warnings.

    @IBAction func Senden(_ sender: Any) {
        let toRecipients = ["Mail@adress.com"]
        let mc: MFMailComposeViewController =       MFMailComposeViewController()
        mc.mailComposeDelegate = self
        mc.setToRecipients(toRecipients)
        mc.setSubject(FirmaFeld.text!)
        
        mc.setMessageBody("Firma: \(FirmaFeld.text!) \n\n Kontaktperson:  \(KontaktpersonFeld.text!) \n\n EMail: \(EMailFeld.text!) \n\n Anliegen:  \(NachrichtFeld.text!)", isHTML: false)
        self.present(mc, animated: true, completion: nil)
    }
    
    func mailComposeController(_ controller:  MFMailComposeViewController, didFinishWith result: MFMailComposeResult,  error: Swift.Error?) {
        controller.dismiss(animated: true, completion: nil)
        
    
        
        }
        
        
    
        
     /*   switch result.rawValue {
        case MFMailComposeResult.cancelled.rawValue:
            print("Mail cancelled")
        case MFMailComposeResult.saved.rawValue:
            print("Mail saved")
        case MFMailComposeResult.sent.rawValue:
            print("Mail sent")
        case MFMailComposeResult.failed.rawValue:
            print("Mail sent failure: %@",   [error!.localizedDescription])
        default:
            break
        }
        // Dismiss the mail compose view controller.
        self.dismiss(animated: true, completion: nil)
        
    }*/
    
    
    @IBAction func dismissKeyboard(_ sender: Any) {
         self.resignFirstResponder()
    }

Solution

  • Show the alertController inside the completionBlock.

    controller.dismiss(animated: true, completion: {
    if result == .cancelled {
            let alertController = UIAlertController(title: "E-Mail not sent!", message: "E-Mail not sent.", preferredStyle: .alert)
    
            alertController.addAction(UIAlertAction(title: "OK", style: .cancel, handler: { (action: UIAlertAction!) in
            }))
    
            present(alertController, animated: true, completion: nil)
        }
    })
    

    Same way when mail send.