Why won't MFMailComposeViewController be closed during email in the app?

I am working on an iPad application that creates a PDF file, which it then sends to the user from the application. I was able to successfully set up email to attach a PDF file, but I cannot remove MFMailComposeViewController. I read several other questions on this subject and tried to imitate what they are doing, but the composer still will not miss. What do I need to change in my code to fire it?

- (IBAction)submitDailyReportButton:(id)sender { MFMailComposeViewController *mail = [[MFMailComposeViewController alloc]init]; [mail setMailComposeDelegate:self]; if ([MFMailComposeViewController canSendMail]) { NSString *email =@ " admin@domain.com "; 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"); } 

}

+4
source share
1 answer

You need to implement the delegate method and reject 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 a mail composer if you cannot send an email. Move the selection inside the if .

+13
source

Source: https://habr.com/ru/post/1481865/


All Articles