xcode 使用 MFMailComposer 通过电子邮件发送 CSV 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10677004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Email CSV file with MFMailComposer
提问by ManOx
I would like to create a file using a NSString (already made) with a .csv extension then email it using the UIMessage framework. So can someone show me the code to create a file (with a .csv extensions and with the contents of a NSString) then how to attach it to a MFMailComposeViewController.
我想使用带有 .csv 扩展名的 NSString(已经制作)创建一个文件,然后使用 UIMessage 框架通过电子邮件发送它。那么有人可以向我展示创建文件的代码(带有 .csv 扩展名和 NSString 的内容),然后如何将它附加到 MFMailComposeViewController。
回答by melsam
This is how you attach a CSV file to a MFMailComposeViewController:
这是将 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.
As far as how to generate the CSV file itself, the CHCSVWriterclass at https://github.com/davedelong/CHCSVParserwill help you.
至于如何生成 CSV 文件本身,https: //github.com/davedelong/CHCSVParser 上的 CHCSVWriter类 将为您提供帮助。
回答by ColossalChris
Here are the parts where you create a new csv, save it to file and attach it all in one. You know, if you're in to that sort of thing
以下是您创建新 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];
}