Ios 使用MFMailComposer通过电子邮件发送CSV文件
我想使用扩展名为.csv的NSString(已创建)创建一个文件,然后使用UIMessage框架通过电子邮件发送。有人能告诉我创建文件(扩展名为.csv,内容为NSString)的代码,然后如何将其附加到MFMailComposeViewController。这是如何将csv文件附加到MFMailComposeViewController的:Ios 使用MFMailComposer通过电子邮件发送CSV文件,ios,xcode,mfmailcomposeviewcontroller,Ios,Xcode,Mfmailcomposeviewcontroller,我想使用扩展名为.csv的NSString(已创建)创建一个文件,然后使用UIMessage框架通过电子邮件发送。有人能告诉我创建文件(扩展名为.csv,内容为NSString)的代码,然后如何将其附加到MFMailComposeViewController。这是如何将csv文件附加到MFMailComposeViewController的: MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc
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文件本身,位于的CHCSVWriter类将帮助您。以下是创建新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];
}
你试过什么?有一个名为
addAttachmentData:mimeType:fileName:
的方法。斯蒂芬·达林顿:是的,我知道如何使用它,但我真的只是想知道如何制作一个CSV文件,然后将其压缩成NSData,以便我可以附加它。