0

In my email composer I would like for the result of 'cancelbuttonclicked' to close the MFMailComposerViewController. Can I implement within the switch statement or do those need to be separate methods. Also, I would like for the send button to send the message before dismissing.

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    switch (result) {
        case MFMailComposeResultSent:{
            UIAlertView *messageSent = [[UIAlertView alloc] initWithTitle:@"Message Sent" message:@"Your message has been sent" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [messageSent show];
        break;
        }
        case MFMailComposeResultSaved:{
            UIAlertView *messageComposeResultSaved = [[UIAlertView alloc] initWithTitle:@"Message Saved" message:@"Your message has been saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [messageComposeResultSaved show];
            break;
        }
        case MFMailComposeResultCancelled:{
            UIAlertView *messageComposeResultCancelled = [[UIAlertView alloc] initWithTitle:@"Message Cancelled" message:@"Your message has been cancelled" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [messageComposeResultCancelled show];
            break;}

        case MFMailComposeResultFailed:{
             UIAlertView *messageFailed = [[UIAlertView alloc]initWithTitle:@"Message Failed" message:@"Your message could not be sent" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [messageFailed show];
            break;
        }
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Michael Castro
  • 314
  • 4
  • 14
  • The code you posted is fine. Did you try it before posting the question? – rmaddy Aug 13 '15 at 18:06
  • I posted an answer with a few suggestions. However, I didn't quite get the "send button" part (the last sentence). Could you please explain a bit more, what the problem is? – FreeNickname Aug 13 '15 at 18:13
  • After the message is sent I would like to dismiss the MFMailCompseViewController. I have edited the code to use UIAlertController. Will you look at [my Gist please](https://gist.github.com/FIDELHIMSELF/303870cb4b137938cea9)? Still no alert view shows. – Michael Castro Aug 13 '15 at 19:11

1 Answers1

1

Your code should work fine. There are no limitations on using UIAlertView inside a switch. However, to make it a bit less messy, I'd suggest rewriting it like this:

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    NSString *alertTitle = nil;
    NSString *alertMessage = nil;
    switch (result) {
        case MFMailComposeResultSent:{
            alertTitle = @"Message Sent";
            alertMessage = @"Your message has been sent";
        break;
        }
        case MFMailComposeResultSaved:{
            alertTitle = @"Message Saved";
            alertMessage = @"Your message has been saved";
            break;
        }
        case MFMailComposeResultCancelled:{
            alertTitle = @"Message Cancelled";
            alertMessage = @"Your message has been cancelled";
            break;}

        case MFMailComposeResultFailed:{
             alertTitle = @"Message Failed";
             alertMessage = @"Your message could not be sent";
            break;
        }
    }
    [[[UIAlertView alloc] initWithTitle:alertTitle 
                                message:alertMessage
                               delegate:self
                      cancelButtonTitle:@"OK"
                      otherButtonTitles:nil] show];
}

It lets you avoid multiple initWithTitle....

But there is another concern: UIAlertView is deprecated is iOS 8. You should use UIAlertController instead. This answer has an example of UIAlertController usage.

Community
  • 1
  • 1
FreeNickname
  • 7,398
  • 2
  • 30
  • 60
  • Can I simply replace 'UIAlertView' with 'UIAlertController'? Also, do I need to handle the 'cancelButton' touchupinside in a separate method? – Michael Castro Aug 13 '15 at 18:47
  • @MichaelCastro, yes, replacing is pretty straightforward in your case. The only significant change is that you don't simply show it. Instead, you present it modally as a `ViewController`. About a cancel button: Do you mean the MFMailComposeViewController's cancel button? If so, then, I believe, it will be handled by `case MFMailComposeResultCancelled`, there is no need for a separate handler. – FreeNickname Aug 13 '15 at 19:11