Swift 在 MFMailComposeViewController didFinishWith 结果中显示警报

Swift show alert in MFMailComposeViewController didFinishWith result

我想在用户取消电子邮件时显示提醒。为此,我使用以下代码:

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {

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

    controller.dismiss(animated: true, completion: nil)
}

该函数已调用,邮件视图已关闭,但未显示任何警报。我在 UITableViewController 中使用这段代码。有人可以帮我吗?

在 completionBlock 中显示 alertController。

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

像这样..