2

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?

3
  • One thing to note, that the mimeType for PDF is application/pdf, not text/pdf. That could be the problem. Feb 1, 2012 at 18:37
  • are you sure of the naming convention ?I mean is the name matching with the PDF file ? Feb 2, 2012 at 5:56
  • yes i double-checked everything, I really have no idea why it's not working
    – AmiiQo
    Feb 2, 2012 at 14:54

2 Answers 2

0

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];
1
  • I was about to say use "NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Stundenplan_1A" ofType:@"pdf"];" but it returns the exact same as what he has. Feb 1, 2012 at 19:02
0

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"];
3
  • still not working, If I send the mail to myself I get an empty email
    – AmiiQo
    Feb 2, 2012 at 14:54
  • 1
    It works now with that code: NSString *path = [[NSBundle mainBundle] pathForResource:@"Stundenplan_1A" ofType:@"pdf"]; NSData *myData = [NSData dataWithContentsOfFile: path]; [viewController addAttachmentData:myData mimeType:@"application/pdf" fileName:@"Stundenplan_1A.pdf"];
    – AmiiQo
    Feb 2, 2012 at 14:59
  • yeah, I also though about your pdf location. NSLog the path, may be you are not getting path properly. File does not contain there. whatever, you solved it!!
    – user3237750
    Apr 7, 2016 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.