2012-07-13 81 views
2

我想从一个URL保存PDF然后通过电子邮件发送。发件人似乎有它,但接收方不明白。保存PDF然后通过电子邮件发送

当我做NSString fileNSLog我得到/var/mobile/Applications/0ADE222E-6346-4C6C-8348-DA5327B980AA/Documents/myPDF.pdf

这似乎是它可以节省,但它不会发送。这是我下面的保存和发送

编辑

更新代码

// This pdfURL is 0 bytes 
    NSData *pdfURL = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",webPage.urlString]]]; 

    //Store the Data locally as PDF File 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentDirectory = [paths objectAtIndex:0]; 
    NSString *file = [documentDirectory stringByAppendingFormat:@"/myPDF.pdf"]; 

    [pdfURL writeToFile:file atomically:YES]; 

    //Sending the pdf 
    MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init]; 
    composer.mailComposeDelegate = self;   
    if ([MFMailComposeViewController canSendMail]){ 
     //Changed email for privacy issues 
     [composer setToRecipients:[NSArray arrayWithObjects:@"[email protected]", nil]]; 
     [composer setSubject:[NSString stringWithFormat:@"%@ email",titleText]]; 
     [composer setMessageBody:@"your custom body content" isHTML:NO]; 

     NSLog(@"pdf %@",file); 
     // This pdfData is 0 bytes 
     NSData *pdfData = [NSData dataWithContentsOfFile:file]; 

     [composer addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"myPDF.pdf"]; 

     [composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve]; 
     [self presentModalViewController:composer animated:YES];    
    } 
+0

你试过没有“/”在你的addAttachmentData调用的文件名部分? – trumpetlicks 2012-07-13 13:26:17

+0

是的,我有。因为之前我读过的东西,我就在那里。它不起作用,虽然 – BigT 2012-07-13 13:30:15

+0

在你打电话给addAttachmentData之前,你能检查你的PDF数据的大小,以确保有真正被读取的东西吗? – trumpetlicks 2012-07-13 13:56:19

回答

0

的问题是与webPage.urlString是空的代码。此代码的工作只需确保URL字符串不为空:P

1

如果从网址的,它存储在NSData的:

NSData *pdfData = [NSData dataWithContentsOfURL[NSURL URLWithString:@"URL Of Pdf"]]; 

然后附上邮寄