开发者

MFMailComposeViewController not dismissing

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 = [NSA开发者_StackOverflow社区rray arrayWithObject:@"contact@app.com"]; 

            [picker setToRecipients:toRecipients];

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

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


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


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;
    


"dismissModalViewControllerAnimated:"is deprecated in iOS 6.0

iOS 7 use:

"dismissViewControllerAnimated:completion:"


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.


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜