Attach a pdf file in-app-mail

走远了吗. 提交于 2019-12-10 23:29:54

问题


I tried to attach my pdf to an in-app-mail. The in-app-mail displays an icon with the pdf but it doesn't send it. I don't know why...

Here's the code:

- (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];    }
}

Any ideas?


回答1:


Have you tried it with?

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

instead of

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

And instead of NSMutableData you could tried it with NSData

NSData *data = [NSData dataWithContentsOfFile:file];



回答2:


Change the mime type like this.

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


来源:https://stackoverflow.com/questions/9101262/attach-a-pdf-file-in-app-mail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!