0

I want to create a csv file with ny string and attaching that. I were try this by using these lines

[foodString writeToFile:@"Meal.csv" atomically:YES encoding:NSUTF8StringEncoding error:NULL];
                [mailView addAttachmentData:NULL mimeType:@"text/csv" fileName:@"Meal.csv"];
                [mailView setMessageBody:@"Open attached file." isHTML:NO];

and MFMail shows me an icon of file in the mail but i couldn't get any file.

Please help me out.Thanx...

Ishu
  • 12,797
  • 5
  • 35
  • 51

3 Answers3

2

Clue ...

addAttachmentData:NULL

You need to actually attach the data. The filename is just the name you are choosing to give the file, it has no other use and doesn't mean attach the file with that name.

The MFMailComposeViewController documentation makes it clear.

You therefore need to do something like this;

NSData *myData = [NSData dataWithContentsOfFile:your-full-file-path];

To generate the data. Note you need to give the full filepath, not just the name (left as an exercise for the reader).

Roger
  • 15,793
  • 4
  • 51
  • 73
0

attachment data cannot be empty so

Try this

[foodString writeToFile:@"Meal.csv" atomically:YES encoding:NSUTF8StringEncoding error:NULL];
[mailView addAttachmentData:[NSData dataWithContentsOfFile:path_to_Meal.csv] mimeType:@"text/csv" fileName:@"Meal.csv"];
[mailView setMessageBody:@"Open attached file." isHTML:NO];
visakh7
  • 26,380
  • 8
  • 55
  • 69
0

Try this

    NSString *filePath = @"write your file path here";

    [foodString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:NULL];
 if([[NSFileManager defaultManager]fileExistsAtPath:filePath]){
    [mailView addAttachmentData:[NSData dataWithContentsOfFile:filePath] mimeType:@"text/csv" fileName:@"Meal.csv"];
    [mailView setMessageBody:@"Open attached file." isHTML:NO];

}
else{
NSLog(@"File not created!");
}
iPhoneDev
  • 1,547
  • 2
  • 13
  • 36