我创建了一个 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 文件。这段代码有什么问题。?为什么接收方没有收到附件。?