如何在iPhone sdk中的MFMailComposer中创建和附加CSV文件?

时间:2012-05-09 05:07:42

标签: iphone csv mfmailcomposeviewcontroller

我创建了一个csv文件,我也将它附加到MFMailComposer,它向我显示我的邮件编辑器,但当我将其发送到用户电子邮件时,它没有显示我在电子邮件中附加csv文件。我已经使用此代码创建了csv文件并在其中添加数据。

        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]);

这里上面的代码工作正常我得到[mediaData length]我从这里附加了CSV文件。

    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];

上面的代码也正常工作。它显示我附加的CSV文件,但当我通过电子邮件发送邮件时,接收器没有收到附加的CSV文件。这段代码有什么问题。为什么接收者没有得到附件。?

3 个答案:

答案 0 :(得分:3)

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];

文字”是字符串。

答案 1 :(得分:0)

我已经通过在MFMailComposeViewController中附加文件和其他媒体属性解决了这个问题。

答案 2 :(得分:0)

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