Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertController won't display - In Swift

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.

like image 991
Nick89 Avatar asked Mar 06 '16 15:03

Nick89


People also ask

How do I show a popup message in Swift?

Adding Action Buttons to Alert Dialog To create an action button that the user can tap on, we will need to create a new instance of an UIAlertAction class and add it to our alert object. To add one more button to UIAlertController simply create a new UIAlertAction object and add it to the alert.

How would I create a UIAlertView in Swift?

var alertView = UIAlertView(); alertView.


2 Answers

I needed an actionSheet rather than an alert, and due to my specific circumstances, none of the solutions here worked. If your UIAlertController isn't showing, an alternative way to do this is to always put it as the topmost window (in Swift 4):

func topmostController() -> UIViewController? {
    if var topController = UIApplication.shared.keyWindow?.rootViewController {
        while let presentedViewController = topController.presentedViewController {
            topController = presentedViewController
        }
        return topController
    }
    return nil
}

// Now put up the menu
let menu=UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
menu.addAction(UIAlertAction(title: "Test", style: .default, handler: {_ in self.doTest()} ))
menu.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: {_ in self.doCancel()} ))
topmostController()?.present(menu, animated: true, completion: nil)

This displays the window regardless of your view controller stack.

like image 168
ajgryc Avatar answered Sep 19 '22 05:09

ajgryc


I was facing the same problem now after presenting an alert in DispachQueue.main.async{}, code is working fine.

like image 34
Anubhav Avatar answered Sep 21 '22 05:09

Anubhav