2

我正在尝试从 url 保存 pdf,然后通过电子邮件发送。发送者似乎有它,但接收者没有得到它。

当我做一个我得到NSLogNSString file/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:@"123@abc.com", 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];            
    }
4

2 回答 2

1

如果它来自 URL,则将其存储在 NSData 中:

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

然后将其附加到邮件

于 2012-07-13T16:28:00.970 回答
0

问题在于 webPage.urlString 为空。此代码只需确保 url 字符串不为空即可:P

于 2012-07-13T14:40:30.860 回答