1

希望标题有意义。这是我的情况。

我在 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 Simulator 文件夹中的 app 文件夹中达到峰值时,pdf 文件就在那里,它们被操作系统识别为 PDF 文件,我可以打开并阅读它们。但是,当我收到一封发送的电子邮件(不要笑,但我们使用 Lotus Notes)时,我无法预览文件当我选择打开它时,我无法显示没有可用过滤器的错误消息(在预览中) 它只是挂起。

我在其他应用程序中使用相同的代码没有问题。我的猜测是我在代码的某个地方做了一些不同的事情。我看不到任何明显的东西,所以我的问题是有没有办法在附加文件之前测试说 NSData 是否成功/失败,或者在附件后测试它,但在电子邮件前测试它?

谢谢

4

1 回答 1

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];

这对我有用。如果您还有其他问题或不清楚,请告诉我。谢谢!

于 2013-07-19T21:03:55.383 回答