我想使用带有 .csv 扩展名的 NSString(已经制作)创建一个文件,然后使用 UIMessage 框架通过电子邮件发送它。所以有人可以告诉我创建文件的代码(带有.csv扩展名和NSString的内容)然后如何将它附加到MFMailComposeViewController。
问问题
5732 次
2 回答
13
这是将 CSV 文件附加到 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.
至于如何生成 CSV 文件本身,https: //github.com/davedelong/CHCSVParser 上的CHCSVWriter类 将为您提供帮助。
于 2012-05-20T21:09:18.007 回答
3
这是您创建新 csv、将其保存到文件并将其全部附加到一个的部分。你知道,如果你喜欢那种事情
NSString *emailTitle = @"My Email Title";
NSString *messageBody = @"Email Body";
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:@[]];
NSMutableString *csv = [NSMutableString stringWithString:@""];
//add your content to the csv
[csv appendFormat:@"MY DATA YADA YADA"];
NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* fileName = @"MyCSVFileName.csv";
NSString* fileAtPath = [filePath stringByAppendingPathComponent:fileName];
if (![[NSFileManager defaultManager] fileExistsAtPath:fileAtPath]) {
[[NSFileManager defaultManager] createFileAtPath:fileAtPath contents:nil attributes:nil];
}
BOOL res = [[csv dataUsingEncoding:NSUTF8StringEncoding] writeToFile:fileAtPath atomically:NO];
if (!res) {
[[[UIAlertView alloc] initWithTitle:@"Error Creating CSV" message:@"Check your permissions to make sure this app can create files so you may email the app data" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles: nil] show];
}else{
NSLog(@"Data saved! File path = %@", fileName);
[mc addAttachmentData:[NSData dataWithContentsOfFile:fileAtPath]
mimeType:@"text/csv"
fileName:@"MyCSVFileName.csv"];
[self presentViewController:mc animated:YES completion:nil];
}
于 2014-09-05T01:59:12.717 回答