次の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出力:
Date、Business、Category、Paid By、Note、Number、Currency、Amount "July 17、2012"、 "tho"、 "Category 1"、 "Cheque"、 "lunch"、 "726269"、 "GBP"、 "100.00 「」
したがって、既存のデータがありますが、正しく添付されていないようですが、ここで何が起こっているのかを誰かが説明できますか?
ありがとう、
ジャック