9.5 C
New York
Tuesday, March 11, 2025

ios – How you can present an alert after transferring to the subsequent view controller?


I am utilizing this code to name MFMailComposeViewController from one other 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.current(detailController, animated: false, completion: nil)
    }
}

MailViewController:

class Mail: UIViewController, MFMailComposeViewControllerDelegate {
    
    var messageTitle = ""
    var messageAddress = ""
    var messageText = ""
    var errorTitle = ""
    var errorMessage = ""
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        ship(title: messageTitle, tackle: [messageAddress], message: messageText, errorTitle: errorTitle, errorMessage: errorMessage)
    }
    
    func ship(title: String, tackle: [String], message: String, errorTitle: String, errorMessage: String) {
        if MFMailComposeViewController.canSendMail() {
            let mail = MFMailComposeViewController()
            mail.mailComposeDelegate = self
            mail.setSubject(title)
            mail.setToRecipients(tackle)
            mail.setMessageBody(message, isHTML: false)
            current(mail, animated: true, completion: nil)
        } else {
            let alert = UIAlertController(title: errorTitle, message: errorMessage, preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "OK", type: .default, handler: dismissHandler))
            self.current(alert, animated: true, completion: nil)
        }
    }
    
    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith outcome: 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 tremendous and I can see MFMailComposeViewController. But when I am unable to ship a message, the UIAlertController shouldn’t be known as and I do not see the alert. How you can repair this?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles