8

I have the following code that gets called in didSelectRowAtIndexPath. The issue is, when I click the cancel button, it prompts for save draft or discard. But when I click either, the view does not dismiss. I've used the same code in a pre iOS 5 app and it dismissed fine. Any ideas? I have the MFMailComposeViewController delegate protocol in the interface.

    if (indexPath.row == 0)
    {
        if([MFMailComposeViewController canSendMail])
        {

            MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
            picker.mailComposeDelegate = self;

            [picker setSubject:@"Support"];

            NSArray *toRecipients = [NSArray arrayWithObject:@"contact@app.com"]; 

            [picker setToRecipients:toRecipients];

            NSString *emailBody = text;
            [picker setMessageBody:emailBody isHTML:NO];

            [self presentModalViewController:picker animated:YES];
        }
    }

5 Answers5

18

Use:

dismissViewControllerAnimated:completion:

DEPRECATED FROM IOS 6.0:

Add this method to your class:

-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    [self dismissModalViewControllerAnimated:YES];
}

Have fun

Or Ron
  • 2,313
  • 20
  • 34
  • This method is deprecated now, Hope this may work instead of above dismissing code...[self dismissViewControllerAnimated:YES completion:nil]; – mavericks Jan 30 '15 at 06:46
7

There could be several problems:

  1. Not adding protocol implemantation in the .h

    @interface yourClass : UIViewController <MFMailComposeViewControllerDelegate>
    
  2. Not adding the relevant function in .m:

    -(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:    (MFMailComposeResult)result error:(NSError*)error {
         [self dismissModalViewControllerAnimated:YES];
    }
    
  3. My error was not setting the correct delegate, but I fixed it :) and now it works for me:

     picker.mailComposeDelegate = self;
    
Luda
  • 7,282
  • 12
  • 79
  • 139
1

"dismissModalViewControllerAnimated:"is deprecated in iOS 6.0

iOS 7 use:

"dismissViewControllerAnimated:completion:"

Wagner Sales
  • 1,448
  • 2
  • 10
  • 14
0

I've described the problem and the way it can be solved more detailed here: https://stackoverflow.com/a/13576408/691660

I am not sure if Luda caught the core of the problem. No difference whether you specify the delegate or not, that does not work in case of modal+modal MFMailComposeViewController instance.

Community
  • 1
  • 1
Agat
  • 4,577
  • 2
  • 34
  • 62
0

Swift Implementation:

Make sure your MFMailComposeViewController protocol and delegate is being called every time its function being executed.

This solves the issue of MFMailComposeViewController not being dismissed.

     let subj = "Test"
     let messageBody = "Test"
     let toRecipents = ["example@xyz.com"]
     let mc: MFMailComposeViewController = MFMailComposeViewController()
     mc.mailComposeDelegate = self
     mc.setSubject(subj)
     mc.setMessageBody(messageBody, isHTML: true)
     mc.setToRecipients(toRecipents)
     self.present(mc, animated: true, completion: nil)
Codetard
  • 2,441
  • 28
  • 34