附上pdf文件in-app-mail

时间:2012-02-01 18:33:27

标签: iphone objective-c email ios5 xcode4.2

我试图将我的pdf附加到应用内邮件中。应用程序内邮件显示带有pdf的图标,但不会发送它。我不知道为什么......

以下是代码:

- (void)openInEmail {

    if ([MFMailComposeViewController canSendMail]) {

        MFMailComposeViewController *viewController = [[MFMailComposeViewController alloc] init];
        viewController.mailComposeDelegate = self;

        [viewController setSubject:@"Stundenplan 1A"];

        NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
        NSString *docDirectory = [sysPaths objectAtIndex:0];
        NSString *filePath = [NSString stringWithFormat:@"%@/Stundenplan_1A.pdf", docDirectory];

        NSMutableData *data=[NSMutableData dataWithContentsOfFile:filePath];

        [viewController addAttachmentData:data mimeType:@"text/pdf" fileName:@"Stundenplan_1A.pdf"];

        [self presentModalViewController:viewController animated:YES];    }
}

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

你试过吗?

NSString *filePath = [documentsDirectory stringByAppendingFileComponent:@"%@/Stundenplan_1A.pdf"];

而不是

NSString *filePath = [NSString stringWithFormat:@"%@/Stundenplan_1A.pdf", docDirectory];

而不是NSMutableData,您可以尝试使用NSData

NSData *data = [NSData dataWithContentsOfFile:file];

答案 1 :(得分:0)

像这样更改mime类型。

NSURL *url = [NSURL URLWithString:pdfURL];
NSData *pdfData = [NSData dataWithContentsOfURL:url];
[mailComposeView addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"CheckList.pdf"];