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
There could be several problems:
Not adding protocol implemantation in the .h
@interface yourClass : UIViewController <MFMailComposeViewControllerDelegate>
Not adding the relevant function in .m:
-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult: (MFMailComposeResult)result error:(NSError*)error {
[self dismissModalViewControllerAnimated:YES];
}
My error was not setting the correct delegate, but I fixed it :) and now it works for me:
picker.mailComposeDelegate = self;
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
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)
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.
"dismissModalViewControllerAnimated:"is deprecated in iOS 6.0
iOS 7 use:
"dismissViewControllerAnimated:completion:"