How to create and attach a CSV file to MFMailComposer in iPhone sdk?

I created one csv file, and I also attach it to MFMailComposer, and it shows me my mail composer, but when I send it to the user's email address, it does not show me the attached csv file by email. I used this code to create a csv file and add data to it.

        NSMutableString *mainString=[[NSMutableString alloc]initWithString:@""];
        //NSMutableArray *section = [[NSMutableArray alloc] init];
        for(int i = 0;i<[NameArray count];i++)
        {
            NSString *string=[indexArray objectAtIndex:i];
            string=[string stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""];
            [mainString appendFormat:@"\"%@\"",string];


            string=[NameArray objectAtIndex:i];  
            string=[string stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""];
            [mainString appendFormat:@",\"%@\"",string];

            string=[typearray objectAtIndex:i];
            string=[string stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""];
            [mainString appendFormat:@",\"%@\"",string];

            [mainString appendFormat:@",\"%@\"",string];
            [mainString appendFormat:@"\n"];

        }

        NSLog(@"getdatafor csv:%@",mainString);

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
        NSString *documentsDirectoryPath = [paths objectAtIndex:0];
        NSString *filePath = [documentsDirectoryPath  stringByAppendingPathComponent:@"history.csv"];
//        filePath = [filePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSData* settingsData;
        settingsData = [mainString dataUsingEncoding: NSASCIIStringEncoding];

        NSError *error;
        [settingsData writeToFile:filePath atomically:YES];
//            NSLog(@"writeok"); 
        NSData *mediaData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMapped   error:&error];

        NSLog(@"Length:%d Error:%@",[mediaData length],[error localizedDescription]);

here the above code works well, I get [mediaData length]. I am attaching a CSV file from here.

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

    // Attach an image to the email
    NSString *path = [[NSBundle mainBundle] pathForResource:@"history" ofType:@"csv"];
    NSData *myData = [NSData dataWithContentsOfFile:path];

    // Fill out the email body text
    NSString *emailBody = @"history";
    [picker setMessageBody:emailBody isHTML:NO];
    [picker addAttachmentData:myData  mimeType:@"text/cvs" fileName:@"history"];

    [self presentModalViewController:picker animated:YES];
    [picker release];

The above code also works correctly. it shows me the attached CSV file, but when I send mail by email, the receiver does not receive the attached CSV file at this time. what's wrong with this code.? why does the recipient not receive the attached file.?

+5
3

- MFMailComposeViewController.

0
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:@"CSV File"];

NSData *myData = [text dataUsingEncoding:NSUTF8StringEncoding];

[mailer addAttachmentData:myData mimeType:@"text/cvs" fileName:@"FileName"];

[self presentModalViewController:mailer animated:YES];

"- .

+3
NSData *data=[[arr componentsJoinedByString:@","] writeToFile:@"Bhavesh.csv" atomically:YES encoding:NSUTF8StringEncoding error:NULL];
[mail addAttachmentData:data mimeType:@"text/csv" fileName:@"Bhavesh.csv"];
0
source

All Articles