如何使用本地文件从iphone应用程序发送电子邮件附件

时间:2013-05-23 09:14:24

标签: iphone email pdf-generation

我想将资源文件夹中的本地文件发送到电子邮件但是它无法正常工作是如何添加本地文件。下面是从iphone的文档文件夹发送电子邮件的代码,但我想从资源文件夹

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSString*giveFileName=@"CPAC_Contract_Equine"; 
NSString *fileName;
//fileName = [[NSString alloc]initWithFormat:@"%@.pdf",giveFileName];

fileName = [[NSString alloc]initWithFormat:@"%@",giveFileName];

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

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];

NSLog(pdfFileName);

NSMutableData *myPdfData = [NSMutableData dataWithContentsOfFile:pdfFileName];
[picker setSubject:@"CPAC Contract for Equine-Sam Veterinarian"];

[picker addAttachmentData:myPdfData mimeType:@"application/pdf" fileName:giveFileName];

[self.navigationController presentViewController:picker animated:YES completion:nil];

1 个答案:

答案 0 :(得分:0)

您发布的代码几乎是正确的,您只需要将文件路径作为应用程序包中文件的路径。替换这部分代码:

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

 NSString *documentsDirectory = [paths objectAtIndex:0];

 NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];

只需从包中加载文件:

 NSString *pdfFileName = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];