Unable to dismiss MFMailComposeViewController, delegate not called
Asked Answered
A

4

53

I am calling MFMailComposeViewController from a UITableViewController. Problem is the delegate method is never called when I select Cancel or Send button in Mail compose 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 doesn't crash. After the Cancel or Send button is pressed, the Compose Window stays on the screen with buttons disabled. I can exit application pressing Home key.

I am able to open other Modal Views form TableView but not MailCompose.

Affine answered 16/12, 2009 at 23:58 Comment(0)
T
219

Make sure you use

controller.mailComposeDelegate = self;

and not

controller.delegate = self;
Tweet answered 25/12, 2010 at 13:47 Comment(2)
MFMailComposeViewController is a subclass of UINavigationController. They do this so that you can implement the UINavigationControllerDelegate methods.Calorie
I think its only one view controller having different delegate, also troubled for some time Thanks mxg :)Linzer
K
14

Your method signature is incorrect:

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

Should be:

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
Kiel answered 17/12, 2009 at 6:25 Comment(1)
I believe this is just a typo in his question.Metage
E
4

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

working code after making removing deprecated one :

#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
Ejective answered 11/3, 2014 at 12:27 Comment(1)
This article is very helpful : ioscreator.com/tutorials/send-email-from-an-appEjective
W
3

I faced the same problem and was searching for a fix from past 2 days then I found a fix myself and you won't believe how minor it was.

In my case the view controller (say 'DetailsTableViewController' as per this question) from where I was presenting the MFMailComposeViewController is already being presented from some other view controller (say 'BaseViewController').

The issue was lying in the 'modalPresentationStyle' of 'DetailsTableViewController' while presenting it from BaseViewController.

The moment I changed it from 'UIModalPresentationFormSheet' to 'UIModalPresentationPageSheet' (for that matter any thing other than 'UIModalPresentationFormSheet') issue got resolved and mail controller delegate methods started firing as usual.

Note: I was already calling the below method in 'DetailsTableViewController' (for this example) so it didn't really matter for me which 'modalPresentationStyle' I was using.

    - (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    self.view.superview.bounds = CGRectMake(0, 0, 1024, 768);
    self.view.superview.backgroundColor = [UIColor clearColor];
}
Wailful answered 29/9, 2016 at 6:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.