gpt4 book ai didi

ios - iOS 7-生成的CSV文件未正确附加到电子邮件

转载 作者:行者123 更新时间:2023-12-01 19:01:42 27 4
gpt4 key购买 nike

抱歉,标题令人困惑。我有一个具有数据库的应用程序。我想我能够将数据库表导出为csv文件。但是,我在附加发送文件时遇到困难。每当我检查yahoo邮件时,都会显示回形针,告诉我有一个附件。但是,当我打开邮件时,邮件中没有文件。

这是我创建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];

}
}

我尝试更改文件路径和文件名,但无济于事。

有任何想法吗?

最佳答案

看起来您正在将CSV文件(或认为自己)写入了您无权访问的路径,即root。

iOS应用已沙箱化,您只能写入某些目录,因此,在这种情况下,应将csv文件写入缓存目录中的某个位置。

更改

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"];

关于ios - iOS 7-生成的CSV文件未正确附加到电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22396260/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com