Email CSV file with MFMailComposer

ios, mfmailcomposeviewcontroller, xcode

Solution

This is how you attach a CSV file to a MFMailComposeViewController:

    MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
    mailer.mailComposeDelegate = self;
    [mailer setSubject:@"CSV File"];        
    [mailer addAttachmentData:[NSData dataWithContentsOfFile:@"PathToFile.csv"]
                     mimeType:@"text/csv" 
                     fileName:@"FileName.csv"];
    [self presentModalViewController:mailer animated:YES];

    // Note: PathToFile.csv is the actual path of the file on your iOS device's 
    // file system. FileName.csv is what it should be displayed as in the email. 

As far as how to generate the CSV file itself, the CHCSVWriter class at https://github.com/davedelong/CHCSVParser will help you.

Problem

I would like to create a file using a NSString (already made) with a .csv extension then email it using the UIMessage framework. So can someone show me the code to create a file (with a .csv extensions and with the contents of a NSString) then how to attach it to a MFMailComposeViewController.

Original source