我正在使用以下 IBAction 将包含一些简单收据数据的 CSV 文件附加到电子邮件中,但是当电子邮件编辑器视图打开时,正文部分中会出现一个灰色文件,并且当我实际发送消息时消息发送时根本没有附件。
这是我的代码:
- (IBAction)actionEmailComposer {
NSLog(@"Receipts Count: %d", [receipts count]);
//Create CSV File
NSMutableString *csvString = [[NSMutableString alloc] init];
[csvString appendString:@"Date,Business,Category,Paid By,Note, Number,Currency, Amount"];
for (int i = 0; i < [receipts count]; i++){
[csvString appendString: [[receipts objectAtIndex:i] receiptDate]];
[csvString appendString: [[receipts objectAtIndex:i] business]];
[csvString appendString: [[receipts objectAtIndex:i] category]];
[csvString appendString: [[receipts objectAtIndex:i] paidBy]];
[csvString appendString: [[receipts objectAtIndex:i] note]];
[csvString appendString: [[receipts objectAtIndex:i] number]];
[csvString appendString: [[receipts objectAtIndex:i] currency]];
[csvString appendString: [[receipts objectAtIndex:i] amount]];
}
[csvString writeToFile:@"test.csv" atomically:YES encoding:NSUTF8StringEncoding error:NULL];
if ([MFMailComposeViewController canSendMail]) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// NSString *recipient = [defaults objectForKey:@"reportsEmail"];
NSString *recipient = @"testemail@live.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:@"test.csv"];
[mailViewController addAttachmentData:myData
mimeType:@"text/csv"
fileName:@"test"];
[self presentModalViewController:mailViewController animated:YES];
}
//Display alert to the user
else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Can't Send Mail"
message:@"Device is unable to send email in its current state."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
csvString 输出:
日期、业务、类别、付款人、备注、数字、货币、金额"2012 年 7 月 17 日","tho","Category 1","Cheque","lunch","726269","GBP","100.00 "
所以有现有的数据,它似乎没有正确附加,谁能解释这里发生了什么?
谢谢,
杰克