Teja Nandamuri
Teja Nandamuri

Reputation: 11201

Unable to attatch a csv file to mail in objective c

I'm using the following code to attach a csv file that is generated to mail composer.

NSString *recipient = @"[email protected]";
NSArray *recipients = [NSArray arrayWithObjects:recipient, nil];

MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:@"CSV Export"];
[mailViewController setToRecipients:recipients];
[mailViewController setMessageBody:@"" isHTML:NO];
mailViewController.navigationBar.tintColor = [UIColor blackColor];
NSData *myData = [NSData dataWithContentsOfFile:@"yourFileName.csv"];

[mailViewController addAttachmentData:myData
                             mimeType:@"text/csv"
                             fileName:@"yourFileName"];

[self presentModalViewController:mailViewController animated:YES];

When the code runs.....the csv file is not attached to the mail properly.

But When I got the mail, it is empty. Could you please help me in finding the issue?

Upvotes: 1

Views: 594

Answers (1)

Alina_cook
Alina_cook

Reputation: 106

[NSData dataWithContentsOfFile:@"yourFileName.csv"]// its wrong way.

Please write full path of csv file Like if your csv file in Documents directory. then

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *csvFilePath = [documentsDirectory stringByAppendingFormat:@"/yourFileName.csv"];

after that change in your code

 [NSData dataWithContentsOfFile:csvFilePath];

Upvotes: 3

Related Questions