Search code examples
iosobjective-cmfmailcomposeviewcontroller

dismissViewControllerAnimated: does not work


I have a method to send an email from my table view.

So one of the cells is a "contact us" cell, and whenever it's tapped I get to the mail view controller using MFMailComposeViewController , but the problem is that when I tap "cancel" or "send" my mail view controller does not go back to the table view. the "cancel" and "send" actions work, but I stay on the mail view controller.

This is the relevant methods:

- (void)sendEmail {
    // Email Subject
    NSString *emailTitle = @"Test Email";
    // Email Content
    NSString *messageBody = @"iOS programming is so fun!";
    // To address
    NSArray *toRecipents = [NSArray arrayWithObject:@"[email protected]"];

    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
    mc.mailComposeDelegate = self;
    [mc setSubject:emailTitle];
    [mc setMessageBody:messageBody isHTML:NO];
    [mc setToRecipients:toRecipents];

    // Present mail view controller on screen
    [self presentViewController:mc animated:YES completion:NULL];

}

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled");

            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail sent");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail sent failure: %@", [error localizedDescription]);
            break;
        default:
            break;
    }

    // Close the Mail Interface
    [self dismissViewControllerAnimated:YES completion:NULL];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    SettingsControllerSection section = indexPath.section;

    if (OtherControllerSection == section) {
        SettingsControllerOtherSection row = indexPath.row;

        switch (row) {

            case RateUsController:
                NSLog(@"rate us was pressed");
                break;

            case ContactUsControllerRow:
                [self sendEmail];
                NSLog(@"send email was pressed");
                break;
        }
    }
}

please help, thanks!!


Solution

  • In mailComposeController method dismiss it like this:

    [controller dismissViewControllerAnimated:YES completion:NULL];