在iOS中的电子邮件中附加pdf文件

时间:2014-07-03 11:05:33

标签: ios pdf sendmail email-attachments

我已经制作了一个pdf文件,我正在尝试将其附加到电子邮件中,但文件没有附加,因为文件名不是什么,它是什么假设。以下是我的代码

- (IBAction)btnAttachPDF:(id)sender {


if ([MFMailComposeViewController canSendMail])
{

    MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];

    mailer.mailComposeDelegate = self;

    [mailer setSubject:@"video"];

    NSArray *toRecipients = [NSArray arrayWithObjects:@"", nil];
    [mailer setToRecipients:toRecipients];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *fileName;
    fileName = [[NSString alloc]initWithFormat:@"%@.pdf", lblName.text];

lblName.text假设要返回我输入的文件名,但它只是.pdf但是如果我给它一个硬编码值,它会存储文件的名称,让我说它给它起了一个名字@“rio。 pdf“但稍后文件没有附加。

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
    NSMutableData *myPdfData = [NSMutableData dataWithContentsOfFile:pdfFileName];
    [mailer addAttachmentData:myPdfData mimeType:@"application/pdf" fileName:fileName];


    [self presentViewController:mailer animated:YES completion:nil];




}



else
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
                                                    message:@"Your device doesn't support the composer sheet"
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];

}

}

1 个答案:

答案 0 :(得分:1)

实际上,NSSearchPathForDirectoriesInDomains返回的路径并不是唯一的路径。它返回的最后一个组件是/ Documents,而文件保存在/Documents/temp/filename.pdf