很抱歉这个令人困惑的标题。我有一个有数据库的应用程序。我想我能够将数据库表导出为csv文件。但是,我在附加发送的文件时遇到了困难。每当我检查我的雅虎邮件时,回形针显示,告诉我那里有附件。但是,当我打开邮件时,邮件中没有文件。
这是我创建csv文件的函数:
- (IBAction)saveToCSV:(id)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *dbPath = [docsPath stringByAppendingPathComponent:@"itemList.db"];
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
[database open];
FMResultSet *results = [database executeQuery:@"SELECT * FROM OrderList"];
//CHCSVWriter *csvWriter = [[CHCSVWriter alloc] initWithCSVFile:@"/csvFile/" atomic:NO];
CHCSVWriter *csvWriter = [[CHCSVWriter alloc] initForWritingToCSVFile:@"/Orderlist.csv"];
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 finishLine];
}
[csvWriter closeStream];
//[csvWriter release];
[database close];
NSLog(@"csv completed");
[self sendEmail];
}
这是我的sendEmail函数
- (void) sendEmail{
if ([MFMailComposeViewController canSendMail]) {
NSArray *sendTo = [NSArray arrayWithObject:@"My Name <myemail@yahoo.com>"];
MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
mailViewController.mailComposeDelegate = self;
[mailViewController setSubject:@"Order CSV."];
[mailViewController setMessageBody:@"Your message goes here." isHTML:NO];
[mailViewController setToRecipients:sendTo];
[mailViewController addAttachmentData:[NSData dataWithContentsOfFile:@"/Orderlist.csv"]
mimeType:@"text/csv"
fileName:@"Orderlist.csv"];
[self presentModalViewController:mailViewController animated:YES];
}
else {
NSLog(@"Device is unable to send email in its current state.");
UIAlertView *message = [[UIAlertView alloc]
initWithTitle:@"Failed to Send Mail!"
message:@"Please make sure that you have logged in to the Mail."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
}
}
我尝试更改文件路径和文件名,但无济于事。
任何想法?
答案 0 :(得分:3)
看起来您正在将CSV文件(或您认为是)写入您无权访问的路径,即root。
iOS应用程序是沙盒的,您只能写入某些目录,因此在您的情况下,您应该将csv文件写入caches目录中的某个位置。
更改
CHCSVWriter *csvWriter = [[CHCSVWriter alloc] initForWritingToCSVFile:@"/Orderlist.csv"];
到
NSString *csvPath = [docsPath stringByAppendingPathComponent:@"Orderlist.csv"];
CHCSVWriter *csvWriter = [[CHCSVWriter alloc] initForWritingToCSVFile:csvPath];
并改变
[mailViewController addAttachmentData:[NSData dataWithContentsOfFile:@"/Orderlist.csv"]
mimeType:@"text/csv"
fileName:@"Orderlist.csv"];
到
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *csvPath = [docsPath stringByAppendingPathComponent:@"Orderlist.csv"];
[mailViewController addAttachmentData:[NSData dataWithContentsOfFile:csvPath]
mimeType:@"text/csv"
fileName:@"Orderlist.csv"];