Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading