Swift 3 How to display a confirmation screen based on MFMailComposeResult email screen

前端 未结 2 1966

I\'m building an app that constructs an email and presents it in a MFMailComposeViewController for the user to send. When the user sends or cancels it I want the app to res

相关标签:
2条回答
  • 2020-11-30 15:24

    You need to make your view controller the MFMailComposeViewController delegate and override the method didFinishWith result and switch MFMailComposeResult value inside the completion handler of the dismiss method :

    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true) { 
            // do whatever you need to do after dismissing the mail window
            switch result {
                case .cancelled: print("cancelled")
                case .saved:     print("saved")
                case .sent:
                    let alert = UIAlertController(title: "Mail Composer", message: "Mail was successfully sent", preferredStyle: .alert)
                    alert.addAction(UIAlertAction(title: "Done", style: .default, handler: nil))
                    self.present(alert, animated: true)
                case .failed:    print("failed")
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 15:27

    If you want to present an alert before the controller is dismissed? Try this one:

    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    
    
        switch result {
        case .cancelled:
    
            let alertController = UIAlertController.init(title: "Cancelled", message: "Some message", preferredStyle: .alert)
            alertController.addAction(UIAlertAction.init(title: "Ok", style: .default, handler: { (alertAction) in
                controller.dismiss(animated: true, completion: nil)
            }))
            controller.present(alertController, animated: true, completion: nil)
    
        case .sent:
    
            let alertController = UIAlertController.init(title: "Sent", message: "Some message", preferredStyle: .alert)
            alertController.addAction(UIAlertAction.init(title: "Ok", style: .default, handler: { (alertAction) in
                controller.dismiss(animated: true, completion: nil)
            }))
            controller.present(alertController, animated: true, completion: nil)
    
        default:
            break;
        }
    }
    
    0 讨论(0)
提交回复
热议问题