MFMailComposeViewController not dismissing
Asked Answered
P

5

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:@"[email protected]"]; 

            [picker setToRecipients:toRecipients];

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

            [self presentModalViewController:picker animated:YES];
        }
    }
Presuppose answered 9/10, 2011 at 21:29 Comment(0)
D
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

Disaccord answered 9/10, 2011 at 22:4 Comment(1)
This method is deprecated now, Hope this may work instead of above dismissing code...[self dismissViewControllerAnimated:YES completion:nil];Polacre
G
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;
    
Graziano answered 4/11, 2012 at 9:29 Comment(0)
C
1

"dismissModalViewControllerAnimated:"is deprecated in iOS 6.0

iOS 7 use:

"dismissViewControllerAnimated:completion:"

Claudineclaudio answered 10/7, 2014 at 16:53 Comment(0)
A
0

I've described the problem and the way it can be solved more detailed here: https://mcmap.net/q/1322140/-presentmodalviewcontroller-crashes-my-app

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.

Ardellaardelle answered 27/11, 2012 at 2:23 Comment(0)
L
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 = ["[email protected]"]
     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)
Libbylibeccio answered 19/12, 2016 at 10:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.