I created ContactUsViewController
.
In this controller, the user will select an option from pickerView
, and then enter the message into textView
and click the "Send Email" button.
When they click the button, it creates an MFMailComposeViewController
to send the email. Now that the email has been sent, saved, canceled, or has worked, the MFMailComposeViewController
closes as it returns to my application. I want a warning to appear next to give them an update on what ever happened. I initially set this using UIAlertView and put 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 used UIAlertView
for email with saved and email. This works absolutely fine and shows my warning as expected.
I recently read that UIAlertView
depreciating with iOS 8, and now we have to use UIAlertController
. So I tried to create the same using the UIAlertController
, which you can see in the "Sent emails" section. However, this does not seem to work, it just does not show any warnings. He prints this error in the logs:
Warning: Attempt to present <UIAlertController: 0x126c50d30> on <XXXX.ContactUsViewController: 0x1269cda70> whose view is not in the window hierarchy!
But I'm not quite sure what that says, or, more importantly, how to fix it.
My questions:
- Am I saying correctly that I cannot use
UIAlertView
? - How can I fix this and make the
UIAlerController
appear after I return from the Mail application?
Thanks in advance.