Question

I am saving file documents directory after that i want to send that file in email but problem is that it doest not get attached i think problem is due path conflict or anything else here is the code where i save file.

// Retrieves the document directories from the iOS device

     NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
     NSString* documentDirectory = [documentDirectories objectAtIndex:0];
     NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

// instructs the mutable data object to write its context to a file on disk

    [pdfData writeToFile:documentDirectoryFilename atomically:YES];
    NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename);

and here is the code which i am using for sending this file in email

    - (NSString *)pathForFile : (NSString *) fileName{

    return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: fileName];
    }

  - (void) sendMailWithAttachedFile : (NSString *) fileName :(NSString *) extension{


MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
//    NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:[self pathForResourse:fileName ofType:extension]];
NSURL* outputURL = [[NSURL alloc] initFileURLWithPath:[self pathForFile:[NSString stringWithFormat:@"%@.%@", fileName, extension]]];
NSData *data=[[NSData alloc]initWithContentsOfURL:outputURL];
[picker addAttachmentData:data mimeType:@"documents/pdf" fileName:@"TestOne.pdf"];
[self presentModalViewController:picker animated:YES];
}


  -(IBAction)onEmailResultPDF{


[self sendMailWithAttachedFile:@"TestOne":@"pdf"];

}
Was it helpful?

Solution

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

NSString *fileName = [[NSString alloc]initWithFormat:@"%@.pdf",giveFileName];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];

NSMutableData *myPdfData = [NSMutableData dataWithContentsOfFile:pdfFileName];
[picker addAttachmentData:myPdfData mimeType:@"application/pdf" fileName:giveFileName];
[self.navigationController presentViewController:picker animated:YES completion:nil];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top