Search code examples
iosswiftuikituialertcontroller

How to show an alert after moving to the next view controller?


I'm using this code to call MFMailComposeViewController from another class:

SomeViewController:

class SomeController: UIViewController {
    func contactWithDeveloper() {
        let detailController = Mail()
        detailController.messageTitle = "1"
        detailController.messageAddress = "1"
        detailController.messageText = "1"
        detailController.errorTitle = "1"
        detailController.errorMessage = "1"
        detailController.modalPresentationStyle = .overFullScreen
        self.present(detailController, animated: false, completion: nil)
    }
}

MailViewController:

class Mail: UIViewController, MFMailComposeViewControllerDelegate {
    
    var messageTitle = ""
    var messageAddress = ""
    var messageText = ""
    var errorTitle = ""
    var errorMessage = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        send(title: messageTitle, address: [messageAddress], message: messageText, errorTitle: errorTitle, errorMessage: errorMessage)
    }
    
    func send(title: String, address: [String], message: String, errorTitle: String, errorMessage: String) {
        if MFMailComposeViewController.canSendMail() {
            let mail = MFMailComposeViewController()
            mail.mailComposeDelegate = self
            mail.setSubject(title)
            mail.setToRecipients(address)
            mail.setMessageBody(message, isHTML: false)
            present(mail, animated: true, completion: nil)
        } else {
            let alert = UIAlertController(title: errorTitle, message: errorMessage, preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: dismissHandler))
            self.present(alert, animated: true, completion: nil)
        }
    }
    
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true, completion: nil)
        dismiss(animated: false, completion: nil)
    }
    
    func dismissHandler(aler: UIAlertAction) {
        dismiss(animated: false, completion: nil)
    }
}

This code snippet if MFMailComposeViewController.canSendMail() works fine and I can see MFMailComposeViewController. But if I can't send a message, the UIAlertController is not called and I don't see the alert. How to fix this?


Solution

  • It is too early to present an alert in viewDidLoad. You can do it in viewWillAppear or viewDidAppear.

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if MFMailComposeViewController.canSendMail() {
            // ...
        } else {
            let alert = UIAlertController(title: errorTitle, message: errorMessage, preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: dismissHandler))
            self.present(alert, animated: true, completion: nil)
        }
    }