1

我想将本地文件从资源文件夹发送到电子邮件,但它不起作用如何添加本地文件。下面是从 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];
4

1 回答 1

0

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

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

 NSString *documentsDirectory = [paths objectAtIndex:0];

 NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];

只需让它从包中加载文件:

 NSString *pdfFileName = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
于 2013-05-23T09:23:59.850 回答