SQLite3 データベースから CSV ファイルを作成し、電子メールで送信したい... CSV ファイルを作成するには、次の関数を使用します。
- (void) exportCSV {
FMDatabase *dbb = [[FMDatabase alloc] initWithPath:[self getDBPath]];
if (![dbb open]) {
//couldn't open the database
[dbb release];
}
FMResultSet *results = [dbb executeQuery:@"SELECT * FROM Daten"];
CHCSVWriter *csvWriter = [[CHCSVWriter alloc] initWithCSVFile:[self getCSVPath] atomic:NO];
while([results next]) {
NSDictionary *resultRow = [results resultDictionary];
NSArray *orderedKeys = [[resultRow allKeys] sortedArrayUsingSelector:@selector(compare:)];
//iterate over the dictionary
for (NSString *columnName in orderedKeys) {
id value = [resultRow objectForKey:columnName];
[csvWriter writeField:value];
}
[csvWriter writeLine];
}
[csvWriter closeFile];
[csvWriter release];
[dbb close];
[dbb release];
}
メール経由でcsvを送信するための私の関数は次のとおりです。
- (void)actionEmailComposer {
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:@"Subject Goes Here"];
[mailViewController setMessageBody:@"Your message goes here" isHTML:NO];
mailViewController.navigationBar.tintColor = [UIColor blackColor];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Speed" ofType:@"csv"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
NSString *fileName = @"Speed.csv";
[mailViewController addAttachmentData:myData mimeType:@"text/csv" fileName:fileName];
[self presentViewController:mailViewController animated:YES completion:nil];
[mailViewController release];
}
else {
NSLog(@"Device is unable to send email in its current state.");
}
}
問題は、私のメールが添付ファイルなしで届くことです... png、jpg、さらには txt などの他の添付ファイルも問題なく!