2013-05-21 4 views
4

나는 사용자가 응용 프로그램에서 전자 메일로 보내는 PDF를 만드는 iPad 응용 프로그램에서 작업하고 있습니다. PDF를 첨부하기 위해 전자 메일을 성공적으로 설정할 수 있었지만 MFMailComposeViewController를 닫을 수 없습니다. 나는이 문제에 관한 몇 가지 다른 질문을 읽고 그들이하고있는 것을 모방하려고 시도했지만 메일 작곡가는 여전히 기각하지 않을 것이다. 해고 할 수 있도록 코드에서 무엇을 변경해야합니까?인앱 이메일 중 MFMailComposeViewController를 닫지 않는 이유는 무엇입니까?

- (IBAction)submitDailyReportButton:(id)sender { 

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

if ([MFMailComposeViewController canSendMail]) { 

    NSString *email [email protected]"[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"); 

} 

}

답변

13

당신은 대리자 메서드를 구현하고 대리자 메서드에서 뷰 컨트롤러를 해제해야합니다.

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

사이드 노트 - 이메일을 보낼 수없는 경우 메일 작성기를 만들 필요가 없습니다. 할당을 if 문 내에서 이동하십시오.

+0

정말 고마워요! 그것은 올바르게 작동합니다. – user2330744

+0

BTW - 이것은 'MFMailComposeViewControllerDelegate' 참조에 설명되어 있습니다. – rmaddy