如何将pdf附加到电子邮件

时间:2013-10-17 09:40:19

标签: ios objective-c

我正在为Ipad做一个应用程序,我正在尝试附加一个pdf文件通过电子邮件发送,为此我创建了这样的pdf。

pageSize= CGSizeMake(612, 792);
    NSString *fileName=@"prueba.pdf";
    NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pdfFileName= [documentsDirectory stringByAppendingPathComponent:fileName];

    NSString *url=[fileName stringByAppendingPathComponent:fileName];
    [self generatePdfWithFilePath:pdfFileName];
    NSData *pdfData = [NSData dataWithContentsOfFile:url];

下一步是创建一封发送pdf的电子邮件,我这样做,就像这样..

   if ([MFMailComposeViewController canSendMail]){
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
        mailer.mailComposeDelegate = self;
        [mailer setSubject:@"esto es para probar"];
        NSArray *toRecipients = [NSArray arrayWithObjects:currentuser.email, nil];
        [mailer setToRecipients:toRecipients];
        [mailer addAttachmentData:pdfData mimeType:@"application/pdf" fileName:fileName];
        NSString *emailBody = @"correo prueba";
        [mailer setMessageBody:emailBody isHTML:NO];
        [self presentModalViewController:mailer animated:YES];
        mailer.modalPresentationStyle = UIModalPresentationPageSheet;

    }else{
...

我生成我创建pdf,一切都是正确的,当我看到电子邮件时,我看到pdf附加像图像(图片),但问题是,当我收到电子邮件时,没有附加任何内容。

任何想法

2 个答案:

答案 0 :(得分:3)

在我看来,在您的代码中,您正在颠倒以下两个步骤的顺序:

NSData *pdfData = [NSData dataWithContentsOfFile:url];
[self generatePdfWithFilePath:pdfFileName];

如果我正确地解释了generatePdfWithFilePath:pdfFileName的含义,我认为这一步应该在另一个之前执行。即:

[self generatePdfWithFilePath:pdfFileName];
NSData *pdfData = [NSData dataWithContentsOfFile:url];

希望这有帮助。

答案 1 :(得分:0)

最后工作,我为别人提出解决方案..谢谢

 pageSize= CGSizeMake(612, 792);
    NSString *fileName=@"prueba.pdf";
    NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pdfFileName= [documentsDirectory stringByAppendingPathComponent:fileName];
    [self generatePdfWithFilePath:pdfFileName];
    NSData *pdfData = [NSData dataWithContentsOfFile:pdfFileName];
    G7DatabaseInterface *databaseInterface = [[DatabaseInterface alloc] init];
    G7User *currentuser=[databaseInterface userByUsername:self.appDelegate.userName];

好的,我解释得更好,对不起,在修复之前我有这个,问题出在这一行

NSString *url=[fileName stringByAppendingPathComponent:fileName];

我的网址是prueba.pdf / prueba.pdf所以路径错了!!

我像这样修理

NSString *pdfFileName= [documentsDirectory stringByAppendingPathComponent:fileName];

并删除* url我不使用它。