1

I'm working on MFMailComposeVC, want to have the function that if user cancel, show alertView and try to let him/her send the email again.

So in MFMailComposeViewControllerDelegate if result status is not Sent, I won't dissmissVC.

Then the funny thing is: User cancel, show alertView, MFVC won't dismiss, then send again, the delegate method won't be triggered. In fact no matter what user do(send, cancel, save draft), the delegate method will only be trigger once.

code sample:

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    if (result == MFMailComposeResultSent){
        [controller dismissViewControllerAnimated:YES completion:NULL];
    } else {
        NSLog(@"do something like show alert");
    }
}

The delegate method will only be trigger once

EDIT: The present mailVC code:

MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
[controller setToRecipients:@"to email"];
[controller setSubject:@"the subject"];
[controller setMessageBody:@"the message body" isHTML:NO];
controller.mailComposeDelegate = self;
controller.modalPresentationStyle = UIModalPresentationFormSheet;
if (controller) {
    [self presentViewController:controller animated:YES completion:nil];
}
Aoke Li
  • 499
  • 3
  • 16
  • I don't understand why are you using `[controller dismissViewControllerAnimated:YES completion:NULL];` to dismiss `MFMailComposeViewController` as it automatically dismisses when user either sends the message or cancels it. – atulkhatri Mar 26 '15 at 06:47
  • In fact you do need to run dismiss. Some other post like: http://stackoverflow.com/a/18697112/2450847 do the same. – Aoke Li Mar 26 '15 at 10:56
  • And that's the funny part that if don't run dismiss, the VC will be there, and delegate can not be trigger again but sending email still working – Aoke Li Mar 26 '15 at 10:57
  • How are you presenting `MFMailComposeViewController`? Please post some more code. – atulkhatri Mar 26 '15 at 12:16
  • edit to add presenting code – Aoke Li Mar 26 '15 at 13:12
  • I figured I was wrong. You need to dismiss `MFMailComposeViewController ` explicitly. I also found out what is wrong with your code, check the answer. – atulkhatri Mar 27 '15 at 05:51

1 Answers1

0

There is one big problem with your previous code. You are dismissing it only when mail is sent successfully but testing it with cancel action.

Use this code for MFMailComposeViewControllerDelegate:

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error 
{ 
    if (result == MFMailComposeResultSent){
        // Removed dismiss code from here
    } else { 
        NSLog(@"do something like show alert"); 
    } 
    [controller dismissViewControllerAnimated:YES completion:NULL]; 
}
atulkhatri
  • 10,896
  • 3
  • 53
  • 89
  • The thing is I want to NOT dismiss mailVC when result in cancel state. In cancel state, either show alterview and let user send it again. Or dismiss mailVC and re-present it to let user send mail again. Anyway, the main purpose is give user a second chance to send email. But either way I try, both can not trigger delegate again. – Aoke Li Mar 27 '15 at 15:03