ejaz dogar
ejaz dogar

Reputation: 115

Attaching a pdf file in an email in iOS

I have made a pdf file and I am trying to attach it with the email but the file doesn't get attached infact the file name isn't what, what it is suppose to be. Following is my code

- (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 suppose to returns me the file name which i have entered but it is just .pdf but if I give it a hardcoded value it does stores the name of the file lets say I gave it a name @"rio.pdf" but later on the file doesn't get attached.

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

}

}

Upvotes: 0

Views: 283

Answers (1)

ejaz dogar
ejaz dogar

Reputation: 115

Actually the path returned by NSSearchPathForDirectoriesInDomains isn't the exact one. The last component it was returning was /Documents whereas, the files were being saved in /Documents/temp/filename.pdf

Upvotes: 1

Related Questions