无法将CSV文件附加到iPhone Mail应用程序

时间:2010-09-15 21:18:27

标签: iphone iphone-sdk-3.0

下面的代码非常适合通过邮件应用程序发送文本(.txt)附件(textfile.csv需要使用textfile.txt随处修改)。但是,当我尝试附加csv文件时,它根本不起作用(邮件应用程序打开显示textfile.csv的图标但是当邮件到达时它没有附件(也没有数据显示在邮件正文中)。如果我将mime类型更改为“text / plain”,我会感到困惑。我很困惑......我做错了什么?有什么想法吗?

    - (NSString *)getPath {
   NSString *paths = [NSSearchPathForDirectoriesInDomains
   (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
   return [paths stringByAppendingPathComponent:@"textfile.csv"];
}

//Saves file to Documents folder
//Method writes a string to a text file
-(void) writeToTextFile{
 //get the documents directory:
 NSArray *paths = NSSearchPathForDirectoriesInDomains
 (NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];


 //make a file name to write the data to using the documents  directory:
 NSString *fileName = [NSString stringWithFormat:@"textfile.csv", 
 documentsDirectory];
 //create content
 NSString *content = @"One,Two,Three,Four,Five";

 //save content to the documents directory
 [content writeToFile:fileName 
     atomically:NO 
    encoding:NSStringEncodingConversionAllowLossy 


 error:nil]; 
}

// Displays an email composition interface inside the application. Populates all the Mail fields.
-(void)displayComposerSheet
{ 
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
 picker.mailComposeDelegate = self; 
 [picker setSubject:@"Subject"];

 // Set up recipients
 NSArray *toRecipients = [NSArray arrayWithObject:@"email@domain.com"];

 [picker setToRecipients:toRecipients];
 // Fill out the email body text
 NSString *emailBody = (@"Data is attached as a .CSV file");

 [picker setMessageBody:emailBody isHTML:NO];

 NSString *SavedDataPath = [self getPath];
 NSString *path = SavedDataPath;

 NSData *myData = [NSData dataWithContentsOfFile:path];
    [picker addAttachmentData:myData mimeType:@"text/csv" fileName:@"textfile.csv"];  

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

3 个答案:

答案 0 :(得分:4)

NSString *str_mail = @"one,two,three";
NSData *myData = [str_mail dataUsingEncoding:NSUTF8StringEncoding]; 

MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
mailController.mailComposeDelegate = self;
[mailController addAttachmentData:myData mimeType:@"text/csv" fileName:@"myfile"];
[mailController setMessageBody:@"body" isHTML:NO];
[self presentModalViewController:mailController animated:YES];

答案 1 :(得分:0)

尝试将MIMETYPE保持为@“mime”。

答案 2 :(得分:0)

[mail addAttachmentData:[[[self eventController] csvDataForEvent:[self event]] dataUsingEncoding:NSUTF8StringEncoding] mimeType:@"text/csv" fileName:[NSString stringWithFormat:@"Product.csv"]];