希望这个标题有意义。这是我的情况。
我在Pages中创建了一个PDF,然后导出为PDF。然后我在应用程序中创建另一个PDF。我使用以下代码创建PDF文件:
- (void)makePDF:(NSString*)fileName :(NSDictionary*)dictResults
{
pageSize = CGSizeMake(612, 792);
_strPDFJustCreated = fileName;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
[self generatePdfWithFilePath:pdfFileName:dictResults];
}
- (void)generatePdfWithFilePath:(NSString *)thefilePath :(NSDictionary*)dictResults
{
UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
BOOL done = NO;
do {
...whole lot of coding goodness
done = YES;
} while (!done);
// Close the PDF context and write the contents out.
UIGraphicsEndPDFContext();
}
使用以下代码将这两个文件附加到电子邮件中:
/**********Attach PDF Files**************/
//get path to pdf file
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* pdfFilePath = [documentsDirectory stringByAppendingPathComponent:strPDFFileName];
//convert pdf file to NSData
NSData* pdfData = [NSData dataWithContentsOfFile:pdfFilePath];
//attach the pdf file
[mailViewController addAttachmentData:pdfData mimeType:@"application/pdf" fileName:strPDFFileName];
//get path to pdf file
NSString* pdf2FilePath = [documentsDirectory stringByAppendingPathComponent:@"OER_MarketingContent.pdf"];
//convert pdf file to NSData
NSData* pdf2Data = [NSData dataWithContentsOfFile:pdf2FilePath];
//attach the pdf file
[mailViewController addAttachmentData:pdf2Data mimeType:@"application/pdf" fileName:@"OER_MarketingContent.pdf"];
[self presentViewController:mailViewController animated:YES completion:NULL];
当我在iPhone模拟器文件夹中的app文件夹中取得峰值时,pdf文件就在那里,它们被操作系统识别为PDF文件,我可以打开并阅读它们。然而,当我收到一封电子邮件消息(并且不笑但我们使用Lotus Notes)时,我无法预览文件我不能发出错误消息,说明没有可用的过滤器,当我选择打开它时(在预览中)它只是挂起。
我在其他应用中使用相同的代码没有任何问题。我的猜测是我在代码中的某个地方做了不同的事情。我看不到任何明显的东西,所以我的问题是有没有办法在附加文件或测试附件之后测试说NSData成功/失败但是预先发送电子邮件?
由于
答案 0 :(得分:0)
请勿使用UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
。相反,请使用:NSData * pdfData = [NSData dataWithContentsOfFile:pdfFileName];
。
所以,整个事情应该最终看起来像:
NSString *fileName = @"Demo.pdf";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
[self generatePdfWithFilePath:pdfFileName];
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setToRecipients:[NSArray arrayWithObject:@"email@email.com"]];
[controller setSubject:@"PDFTEST"];
[controller setMessageBody:@"Your text here" isHTML:NO];
NSData * pdfData = [NSData dataWithContentsOfFile:pdfFileName];
NSString *testFileName = [NSString stringWithFormat:@"PDFNAME"];
[controller addAttachmentData:pdfData mimeType:@"application/pdf" fileName:testFileName];
[self presentModalViewController:controller animated:YES];
这对我有用。如果您有任何疑问或者不清楚,请与我们联系。谢谢!