保存PDF然后发送电子邮件

时间:2012-07-13 12:56:40

标签: iphone xcode mfmailcomposeviewcontroller

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

当我NSLog NSString 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];            
    }

2 个答案:

答案 0 :(得分:1)

如果是来自URL,请将其存储在NSData:

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

然后将其附加到邮件

答案 1 :(得分:0)

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