Unable to dismiss MFMailComposeViewController, delegate not called

前端 未结 4 1063
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 12:45

I am calling MFMailComposeViewController from a UITableViewController. Problem is the delegate method is never called when I select Cancel or

相关标签:
4条回答
  • 2021-01-30 13:17

    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];
    }
    
    0 讨论(0)
  • 2021-01-30 13:20

    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
    
    0 讨论(0)
  • 2021-01-30 13:20

    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
    
    0 讨论(0)
  • 2021-01-30 13:34

    Make sure you use

    controller.mailComposeDelegate = self;
    

    and not

    controller.delegate = self;
    
    0 讨论(0)
提交回复
热议问题