Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't MFMailComposeViewController dismiss during in-app email?

I am working on an iPad app which creates a PDF which the user then emails in from the app. I have been able to successfully setup the email to attach the PDF, but I cannot get the MFMailComposeViewController to dismiss. I have read through several other questions on this issue and attempted to mimic what they are doing, but the mail composer still will not dismiss. What do I need to change in my code to get it to dismiss?

- (IBAction)submitDailyReportButton:(id)sender {

MFMailComposeViewController *mail = [[MFMailComposeViewController alloc]init];
[mail setMailComposeDelegate:self];

if ([MFMailComposeViewController canSendMail]) {

    NSString *email =@"[email protected]";
    NSArray *emailArray = [[NSArray alloc]initWithObjects:email,nil];
    [mail setToRecipients:emailArray];

    [mail setSubject:@"Daily Report"];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *newFilePath = [[paths objectAtIndex:0]stringByAppendingPathComponent:@"report.pdf"];

    NSData *pdfData = [NSData dataWithContentsOfFile:newFilePath];
    [mail addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"report.pdf"];
    NSString *body = @"Please review the attached daily report";
    [mail setMessageBody:body isHTML:NO];
    [mail setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [self presentViewController:mail animated:YES completion:nil];

}else{

    NSLog(@"Message cannot be sent");

}

}

like image 835
BBringardner Avatar asked Dec 11 '22 15:12

BBringardner


1 Answers

You have to implement the delegate method and dismiss the view controller in the delegate method.

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
    [self dismissViewControllerAnimated:YES completion:nil];
}

Side note - there is no need to create the mail composer if you can't send email. Move the allocation inside the if statement.

like image 120
rmaddy Avatar answered Feb 01 '23 08:02

rmaddy