gpt4 book ai didi

iphone - 将 CSV 文件附加到电子邮件目标 - C

转载 作者:行者123 更新时间:2023-11-28 18:40:29 24 4
gpt4 key购买 nike

我正在使用以下 IBAction 将包含一些简单收据数据的 CSV 文件附加到电子邮件中,但是当电子邮件编辑器 View 打开时,正文部分会出现一个灰色文件,而当我实际上发送消息发送的消息,完全没有附件。

这是我的代码:

    - (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”、“类别 1”、“支票”、“午餐”、“726269”、“GBP” "100.00"

所以存在现有数据,但它似乎没有正确附加,任何人都可以解释这里发生了什么吗?

谢谢,

jack

最佳答案

试试这段代码

            arr_email=[[NSMutableArray alloc] initWithArray:[objDatabase lookupAllForSQL:@"select * from password"]];


NSString *mystr=@"";
NSString *str_type;
NSString *str_name;
NSString *str_user;
NSString *str_pwd;
NSString *str_url;
NSString *str_descr;
NSString *csvstr;

csvstr=[NSString stringWithFormat:@"Account Type,Account Name,UserName/Card No,Password/Pin No, URL/Other Info,Description"];

for (int i=0; i<[arr_email count]; i++) {
NSMutableDictionary *dict = [arr_email objectAtIndex:i];

str_type=[NSString stringWithFormat:@"%@",[dict objectForKey:@"AccountType"]];
str_name=[NSString stringWithFormat:@"%@",[dict objectForKey:@"AccountName"]];
str_user=[NSString stringWithFormat:@"%@",[dict objectForKey:@"UserName"]];
str_pwd=[NSString stringWithFormat:@"%@",[dict objectForKey:@"Password"]];
str_url=[NSString stringWithFormat:@"%@",[dict objectForKey:@"Url"]];
str_descr=[NSString stringWithFormat:@"%@",[dict objectForKey:@"Description"]];

mystr=[NSString stringWithFormat:@"%@ \n %@,%@,%@,%@,%@,%@",mystr,str_type,str_name,str_user,str_pwd,str_url,str_descr];

}
csvstr=[NSString stringWithFormat:@"%@ \n %@",csvstr,mystr];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];

NSString *filename = [docDir stringByAppendingPathComponent:[NSString stringWithFormat:@"My Private Closet.csv"]];
NSError *error = NULL;
BOOL written = [csvstr writeToFile:filename atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (!written)
NSLog(@"write failed, error=%@", error);

关于iphone - 将 CSV 文件附加到电子邮件目标 - C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11524548/

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