Failed to reject MFMailComposeViewController, delegate not called

I call MFMailComposeViewController from UITableViewController . The problem is that the delegate method is never called when I select the Cancel or Submit button in the Write Mail window:

 mailComposeController:(MFMailComposeViewController*)controllerdidFinishWithResult 

Here is the table view class:

 @implementation DetailsTableViewController - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section==0 && indexPath.row==4) { //SEND MAIL MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init]; controller.mailComposeDelegate = self; if ([MFMailComposeViewController canSendMail]) { [controller setSubject:[NSString stringWithFormat:@"Ref %@",[item objectForKey:@"reference"]]]; [controller setMessageBody:@" " isHTML:NO]; [controller setToRecipients:[NSArray arrayWithObject:[item objectForKey:@"email"]]]; [self presentModalViewController:controller animated:YES]; } [controller release]; } } - (void)mailComposeController:(MFMailComposeViewController*)controllerdidFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { // NEVER REACHES THIS PLACE [self dismissModalViewControllerAnimated:YES]; NSLog (@"mail finished"); } 

The application does not crash. After clicking the “Cancel” or “Send” button, the “Component” window remains on the screen with the buttons disabled. I can exit the application by pressing the "Home" key.

I can open another Modal Views TableView form, but not MailCompose.

+43
ios objective-c cocoa-touch mfmailcomposeviewcontroller
Dec 16 '09 at 23:58
source share
4 answers

Make sure you use

 controller.mailComposeDelegate = self; 

but not

 controller.delegate = self; 
+199
Dec 25 '10 at 13:47
source share

Your method signature is incorrect:

 - (void)mailComposeController:(MFMailComposeViewController*)controllerdidFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 

Must be:

 - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
+13
Dec 17 '09 at 6:25
source share

See this article for full implementation: http://www.ioscreator.com/tutorials/send-email-from-an-app

working code after removing obsolete:

 #import <MessageUI/MFMailComposeViewController.h> @interface SettingsTableViewController () <MFMailComposeViewControllerDelegate, UITextFieldDelegate, UITextViewDelegate> @end @implementation SettingsTableViewController // add default methods -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSInteger sectionNum = indexPath.section; NSInteger rowNum = indexPath.row; if (sectionNum == 2 && rowNum == 1) { MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init]; controller.mailComposeDelegate = self; if ([MFMailComposeViewController canSendMail]) { [controller setSubject:[NSString stringWithFormat:@"Invitation to Northstar app"]]; [controller setMessageBody:@" " isHTML:NO]; // [controller setToRecipients:[NSArray arrayWithObject:[item objectForKey:@"email"]]]; //presentViewController:animated:completion: [self presentViewController:controller animated:YES completion:NULL]; } } } - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { NSLog (@"mail finished"); [self dismissViewControllerAnimated:YES completion:NULL]; } @end 
+4
Mar 11 '14 at 12:27
source share

I ran into the same problem and searched for a fix in the last 2 days, after which I myself found a fix and you won’t believe how insignificant it was.

In my case, the view controller (say, “DetailsTableViewController” according to this question), where I represented the MFMailComposeViewController , is already presented from some other view controller (for example, “BaseViewController”).

The problem lay in the ' modalPresentationStyle ' of the DetailsTableViewController, presenting it from the BaseViewController.

At that moment, when I changed it from " UIModalPresentationFormSheet " to " UIModalPresentationPageSheet " (for that matter, other than " UIModalPresentationFormSheet "), the problem was resolved and the delegation methods of the mail controller started to shoot as usual.

Note. I already called the method below in the DetailsTableViewController (for this example), so it didn't matter to me what “ modalPresentationStyle ” I used.

  - (void)viewWillLayoutSubviews{ [super viewWillLayoutSubviews]; self.view.superview.bounds = CGRectMake(0, 0, 1024, 768); self.view.superview.backgroundColor = [UIColor clearColor]; } 
0
Sep 29 '16 at 6:31
source share



All Articles