我正在使用以下代码通过电子邮件发送csv
。电子邮件是发送的,我在收件箱中收到了,但我没有收到附件中的csv
文件,对错误有任何疑问?
-(void)generateCSV
{
NSFileManager *filemgr = [NSFileManager defaultManager];
NSArray *dirPaths;
NSString *docsDir;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
if ([filemgr fileExistsAtPath:[NSString stringWithFormat:@"%@/test.csv", docsDir] ] == NO)
{
[filemgr createFileAtPath:[NSString stringWithFormat:@"%@/test.csv", docsDir] contents:[@"" dataUsingEncoding: NSUnicodeStringEncoding] attributes: nil];
}
surveyarray = [db retrieveSurvey];
CHCSVWriter * csvWriter = [[CHCSVWriter alloc] initWithCSVFile:[NSString stringWithFormat:@"%@/test.csv", docsDir] atomic:NO];
NSString *str = [NSString stringWithFormat: @"No, CreatedDate, Question1, Question2, Question3, Comment\n"];
[csvWriter writeField:str];
for(int i = 0; i < surveyarray.count; i++)
{
survey = [surveyarray objectAtIndex:i];
NSString *str = [NSString stringWithFormat: @"%d, %@, %@, %@, %@, %@\n", i, survey.createddate, survey.question1, survey.question2, survey.question3, survey.comment];
[csvWriter writeField:str];
}
NSMutableString *subject = [[NSMutableString alloc] init];
NSMutableString *emailBody = [[NSMutableString alloc] init];
[subject appendString:@""];
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:subject];
NSArray *toRecipients = [NSArray arrayWithObjects:@"", nil];
[mailer setToRecipients:toRecipients];
NSData *myData = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/test.csv", docsDir]];
NSLog(@"%@", myData);
[mailer addAttachmentData:myData mimeType:@"text/csv" fileName:@"test.csv"];
[mailer setMessageBody:emailBody isHTML:NO];
mailer.modalPresentationStyle = UIModalPresentationPageSheet;
[self presentModalViewController:mailer animated:YES];
[mailer release];
[subject release];
[emailBody release];
[csvWriter release];
}
我正在生成csv
个文件,然后以附件形式发送电子邮件。
我在邮箱中打开电子邮件,它不包含任何附件。
答案 0 :(得分:0)
根据我看到的there,我会说您忘记在文件名末尾添加.csv
:)
答案 1 :(得分:0)
根据NSData类引用,dataWithContentsOfFile:给出的路径应该是绝对路径。所以你应该使用
[NSString stringWithFormat:@"%@/test.csv", docsDir]
而不只是@"test.csv"
我打赌你的myData
是零或空的。
顺便说一下,我只用一次完全限定的路径/文件名创建一个NSString对象并重新使用它而不是创建它3到4次。
答案 2 :(得分:0)
这段代码每次都适合我!
http://developer.apple.com/library/ios/#samplecode/MailComposer/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008865
答案 3 :(得分:-1)
尝试替换上面这行
[mailer addAttachmentData:myData mimeType:@"text/csv" fileName:@"test.csv"];
答案 4 :(得分:-1)
最后我解决了这个问题。电子邮件附加附件只是文件是空的。这是因为for循环初始化为-1而i变为int i = 0,它现在正常工作