UIAlertController won't display - In Swift

Nick89 picture Nick89 · Mar 6, 2016 · Viewed 10.9k times · Source

I have created a ContactUsViewController.

In this controller the user will select a option from a pickerView and then type a message in a textView and press the Send Email button.

When they press the button, it creates a MFMailComposeViewController so they can send the email. Now, when the email is either Sent, Saved, Cancelled or Failed, the MFMailComposeViewController closes as they are back in my app. I want an alert to then appear to give them an update, on what ever just happened. I have originally set this up using a UIAlertView, and have placed this code in the fun mailComposeController function, see below:

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {

    switch result.rawValue {

    case MFMailComposeResultCancelled.rawValue:
        NSLog("Email cancelled")
    case MFMailComposeResultSaved.rawValue:
        NSLog("Email saved")
        let sendMailErrorAlert = UIAlertView(title: "Email saved", message: "Your email has been saved in Mail.", delegate: self, cancelButtonTitle: "OK")
        sendMailErrorAlert.show()
    case MFMailComposeResultSent.rawValue:
        NSLog("Email sent")

        let alertController = UIAlertController(title: "test", message: "test", preferredStyle: .Alert)
        let okButton = UIAlertAction(title: "Okay", style: .Default, handler: nil)
        alertController.addAction(okButton)
        presentViewController(alertController, animated: true, completion: nil)

    case MFMailComposeResultFailed.rawValue:
        NSLog("Email failed: %@", [error!.localizedDescription])
        let sendMailErrorAlert = UIAlertView(title: "Oops!", message: "Looks like something went wrong, and the email couldn't send. Please try again later.", delegate: self, cancelButtonTitle: "OK")
        sendMailErrorAlert.show()
    default:
        break

    }

As you can see, I have used the UIAlertView for Email Saved and Email failed. This works absolutely fine, and shows my alert as expected.

I recently read that UIAlertView is depreciated since iOS 8, and that we should now use UIAlertController. Therefore I tried creating the same thing using the UIAlertController, which you can see for the Email Sent section. However, this doesn't seem to work, it just doesn't show any alert. It does print this error into the logs:

Warning: Attempt to present <UIAlertController: 0x126c50d30> on <XXXX.ContactUsViewController: 0x1269cda70> whose view is not in the window hierarchy! 

But I'm not really sure what this is saying, or more importantly, how to fix it.

My questions are:

  1. Am I right in saying I cannot use UIAlertView?
  2. How can I fix this and make the UIAlerController appear after I have returned from the Mail app?

Thanks in advance.

Answer

Yuchen Zhong picture Yuchen Zhong · Mar 6, 2016

The problem is that by the time you call this following function:

present(alertController, animated: true, completion: nil)

your mail view controller is still visible. You have to make sure that the alertController is presented on the top of window hierarchy. In your example, you have the following window hierarchy:

+---------------------------+
| MailComposeViewController |
+---------------------------+
            ||
+---------------------------+
|  ContactUsViewController  |
+---------------------------+

What you should do instead is to dismiss the email view controller first. When that is done, show you alert view controller.

let alertController = UIAlertController(title: "test", message: "test", preferredStyle: .Alert)
let okButton = UIAlertAction(title: "Okay", style: .Default, handler: nil)
alertController.addAction(okButton)
controller.dismissViewControllerAnimated(true){ () -> Void in
     self.present(alertController, animated: true, completion: nil)   
}

Alternatively, you can also present your alertController on top of the MailComposeViewController, like so:

let alertController = UIAlertController(title: "test", message: "test", preferredStyle: .Alert)
let okButton = UIAlertAction(title: "Okay", style: .Default, handler: nil)
alertController.addAction(okButton)
controller.present(alertController, animated: true, completion: nil)