无法将csv文件附加到目标c中的邮件中

时间:2014-06-29 14:37:26

标签: ios email csv

我正在使用以下代码将生成的csv文件附加到邮件编辑器。

NSString *recipient = @"tejanvm92@gmail.com";
NSArray *recipients = [NSArray arrayWithObjects:recipient, nil];

MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:@"CSV Export"];
[mailViewController setToRecipients:recipients];
[mailViewController setMessageBody:@"" isHTML:NO];
mailViewController.navigationBar.tintColor = [UIColor blackColor];
NSData *myData = [NSData dataWithContentsOfFile:@"yourFileName.csv"];

[mailViewController addAttachmentData:myData
                             mimeType:@"text/csv"
                             fileName:@"yourFileName"];

[self presentModalViewController:mailViewController animated:YES];

代码运行时...... csv文件未正确附加到邮件中。

但是当我收到邮件时,它是空的。你能帮我找到这个问题吗?

1 个答案:

答案 0 :(得分:3)

[NSData dataWithContentsOfFile:@"yourFileName.csv"]// its wrong way.

请写下csv文件的完整路径 就像你的csv文件在Documents目录中一样。 然后

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *csvFilePath = [documentsDirectory stringByAppendingFormat:@"/yourFileName.csv"];

在您的代码更改后

 [NSData dataWithContentsOfFile:csvFilePath];