Iphone 将CSV文件附加到电子邮件目标的问题-C

Iphone 将CSV文件附加到电子邮件目标的问题-C,iphone,objective-c,ios,csv,mfmailcomposeviewcontroller,Iphone,Objective C,Ios,Csv,Mfmailcomposeviewcontroller,我正在使用以下iAction将包含一些简单回执数据的CSV文件附加到电子邮件中,但是,当电子邮件编写器视图打开时,正文部分会显示一个灰色文件,并且当我实际发送消息时,该消息发送时根本没有附件 这是我的密码: - (IBAction)actionEmailComposer { NSLog(@"Receipts Count: %d", [receipts count]); //Create CSV File NSMutableString *csvString = [

我正在使用以下iAction将包含一些简单回执数据的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];
    }

}
-(iAction)actionEmailComposer{
NSLog(@“收据计数:%d”,[收据计数]);
//创建CSV文件
NSMutableString*csvString=[[NSMutableString alloc]init];
[csvString appendString:@“日期、业务、类别、付款人、票据、编号、货币、金额”];
对于(int i=0;i<[收据计数];i++){
[csvString appendString:[[receipts objectAtIndex:i]receiptDate]];
[csvString appendString:[[receipts objectAtIndex:i]business]];
[csvString appendString:[[receipts objectAtIndex:i]类别]];
[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]金额]];
}
[csvString writeToFile:@“test.csv”原子:是编码:NSUTF8StringEncoding错误:NULL];
如果([MFMailComposeViewController canSendMail]){
NSUserDefaults*默认值=[NSUserDefaults standardUserDefaults];
//NSString*recipient=[defaults objectForKey:@“reportsEmail”];
NSString*收件人=@“testemail@live.com";
NSArray*recipients=[NSArray arrayWithObjects:recipient,nil];
MFMailComposeViewController*mailViewController=[[MFMailComposeViewController alloc]init];
mailViewController.mailComposeDelegate=self;
[mailViewController设置主题:@“CSV导出”];
[mailViewController设置收件人:收件人];
[mailViewController setMessageBody:@“isHTML:否];
mailViewController.navigationBar.tintColor=[UIColor blackColor];
NSData*myData=[NSData dataWithContentsOfFile:@“test.csv”];
[mailViewController addAttachmentData:myData
mimeType:@“text/csv”
文件名:@“测试”];
[self-presentModalViewController:mailViewController动画:是];
}
//向用户显示警报
否则{
UIAlertView*alertView=[[UIAlertView alloc]initWithTitle:@“无法发送邮件”
消息:@“设备无法在其当前状态下发送电子邮件。”
代表:无
取消按钮:@“确定”
其他按钮:无];
[警报视图显示];
}
}
csvString输出:

日期、业务、类别、付款人、票据、编号、货币、金额“2012年7月17日”、“tho”、“1类”、“支票”、“午餐”、“726269”、“英镑”、“100.00”

所以有现有的数据,它似乎没有正确地连接,有人能解释这里发生了什么吗

谢谢


Jack

这是违反直觉的,但是附加文件时的文件名不包括扩展名。因此,使用名为“test.csv”的文件名:


这与直觉相反,但附加文件时的文件名不包括扩展名。因此,使用名为“test.csv”的文件名:

试试这个代码

            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);
arr_email=[[NSMutableArray alloc]initWithArray:[objDatabase lookupAllForSQL:@“从密码中选择*”];
NSString*mystr=@;
NSString*str_类型;
NSString*str_名称;
NSString*str_用户;
NSString*str_pwd;
NSString*str_url;
NSString*STRU descr;
NSString*csvstr;
csvstr=[NSString stringWithFormat:@“帐户类型、帐户名称、用户名/卡号、密码/Pin号、URL/其他信息、说明”];
对于(inti=0;i请尝试以下代码

            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);
arr_email=[[NSMutableArray alloc]initWithArray:[objDatabase lookupAllForSQL:@“从密码中选择*”];
NSString*mystr=@;
NSString*str_类型;
NSString*str_名称;
NSString*str_用户;
NSString*str_pwd;
NSString*str_url;
NSString*STRU descr;
NSString*csvstr;
csvstr=[NSString stringWithFormat:@“帐户类型、帐户名称、用户名/卡号、密码/Pin号、URL/其他信息、说明”];

对于(int i=0;这是一种构建
stringToWrite
的糟糕方法。为了我的心情平静,请至少使用一个可变字符串并附加到该字符串。好的,我编辑了我的代码以让您心情平和,谢谢。谢谢,我感到放心了;)我想你留下引号是为了简洁?这是构建
stringToWrite
的糟糕方法。为了我的心情平静,请至少使用一个可变字符串并附加到该字符串上。好的,我编辑了我的代码是为了你的心情平静,谢谢。谢谢,我感到宽慰;)我想你留下引号是为了简洁?嗨,谢谢你的回答,但这并没有改变任何事情。。我已经编辑了我的代码以合并更改。您能验证文件是否已写入并包含正确的数据吗?另外,请确保
myData
包含某些内容。(我使用了
dataWithContentsOfURL
,它很有效。)嗨,谢谢你的回答,但它没有改变任何东西。。我已经编辑了我的代码以合并更改。您能验证文件是否已写入并包含正确的数据吗?另外,请确保
myData
包含某些内容。(我使用了
dataWithContentsOfURL
,它成功了。)