所以我有一段时间遇到这个问题而且无法让它运转起来!我一直在构建一个调查应用程序,用户只需输入信息并将其保存到csv文件中。我现在处于需要将应用程序中的csv文件附加到电子邮件地址的阶段......
我刚在新的i-phone上测试了这个,收到电子邮件时没有附件?它在邮件应用程序和模拟器中,但是当在电子邮件帐户上收到邮件时附件已经消失了?谁能帮忙?我的代码如下:
- (IBAction)send:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedFilePath = [documentsDirectory stringByAppendingPathComponent:@"results.csv"];
NSData *csvData = [NSData dataWithContentsOfFile:savedFilePath];
MFMailComposeViewController *mailcomposer = [[MFMailComposeViewController alloc] init];
[mailcomposer addAttachmentData:csvData mimeType:@"text/csv" fileName:@"results.csv"];
[mailcomposer setToRecipients:@[@"gpsflighttrial@gcap.eu"]];
[mailcomposer setSubject:self.subject.text];
[mailcomposer setMessageBody:self.message.text isHTML:NO];
}
答案 0 :(得分:0)
将您的mimeType更改为@" application / csv"它会起作用。
答案 1 :(得分:0)
未附加csv的原因可以是以下的以太:
1)filename
或filepath
为wrong
,因为csv的结果nsdata
将为nil
。
2)filename
方法中的addAttachmentData mimeType: fileName:
应该only name
而不是file format
的类型。
方法的变化如下:
-(IBAction)send:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Determine the file name and extension
NSString *strFileName = @"results.csv";
NSArray *filepart = [strFileName componentsSeparatedByString:@"."];
NSString *filename = [filepart objectAtIndex:0];
//get file path
NSString *savedFilePath = [documentsDirectory stringByAppendingPathComponent:strFileName];
//Now check if file exits
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:savedFilePath]){
NSData *csvData = [NSData dataWithContentsOfFile:savedFilePath];
if(csvData)
{
MFMailComposeViewController *mailcomposer = [[MFMailComposeViewController alloc] init];
[mailcomposer addAttachmentData:csvData mimeType:@"text/csv" fileName:filename];
[mailcomposer setToRecipients:@[@"gpsflighttrial@gcap.eu"]];
[mailcomposer setSubject:self.subject.text];
[mailcomposer setMessageBody:self.message.text isHTML:NO];
}
else
NSLog(@"error csv data not created");
}
else
NSLog(@"error file doesnot exists");
}
答案 2 :(得分:0)
所以经过许多不眠之夜和许多尝试不同的方式后,我终于找到了与朋友合作的方式(见下面的代码)
NSString *docsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSData *attachment = [NSData dataWithContentsOfFile:[docsDirectory stringByAppendingPathComponent:@"results.csv"]];
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setToRecipients:@[@"gpsflighttrial@gcap.eu"]];
[mailer setSubject:self.subject.text];
[mailer setMessageBody:self.message.text isHTML:NO];
[mailer addAttachmentData:attachment mimeType:@"application/csv" fileName:@"results.csv"];
[self presentModalViewController:mailer animated:YES];
以防万一将来有这个问题。