gpt4 book ai didi

iphone - 如何在 iPhone sdk 的 MFMailComposer 中创建并附加 CSV 文件?

转载 作者:行者123 更新时间:2023-12-03 18:59:29 25 4
gpt4 key购买 nike

我已经创建了一个 csv 文件,并且还将其附加到 MFMailComposer,它向我的邮件编辑器显示,但当我将其发送到用户电子邮件时,它不会向我显示电子邮件中附加的 csv 文件。我已使用此代码创建 csv 文件并在其中添加数据。

        NSMutableString *mainString=[[NSMutableString alloc]initWithString:@""];
//NSMutableArray *section = [[NSMutableArray alloc] init];
for(int i = 0;i<[NameArray count];i++)
{
NSString *string=[indexArray objectAtIndex:i];
string=[string stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""];
[mainString appendFormat:@"\"%@\"",string];


string=[NameArray objectAtIndex:i];
string=[string stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""];
[mainString appendFormat:@",\"%@\"",string];

string=[typearray objectAtIndex:i];
string=[string stringByReplacingOccurrencesOfString:@"\"" withString:@"\"\""];
[mainString appendFormat:@",\"%@\"",string];

[mainString appendFormat:@",\"%@\"",string];
[mainString appendFormat:@"\n"];

}

NSLog(@"getdatafor csv:%@",mainString);

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:@"history.csv"];
// filePath = [filePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData* settingsData;
settingsData = [mainString dataUsingEncoding: NSASCIIStringEncoding];

NSError *error;
[settingsData writeToFile:filePath atomically:YES];
// NSLog(@"writeok");
NSData *mediaData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMapped error:&error];

NSLog(@"Length:%d Error:%@",[mediaData length],[error localizedDescription]);

上面的代码运行良好,我得到了 [mediaData length],我从这里附加了 CSV 文件。

    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;

// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:@"history" ofType:@"csv"];
NSData *myData = [NSData dataWithContentsOfFile:path];

// Fill out the email body text
NSString *emailBody = @"history";
[picker setMessageBody:emailBody isHTML:NO];
[picker addAttachmentData:myData mimeType:@"text/cvs" fileName:@"history"];

[self presentModalViewController:picker animated:YES];
[picker release];

上面的代码也可以正常工作。它向我显示了附加的 CSV 文件,但是当我通过电子邮件发送邮件时,收件人没有收到附加的 CSV 文件。这段代码有什么问题。?为什么接收者没有收到附件。?

最佳答案

MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:@"CSV File"];

NSData *myData = [text dataUsingEncoding:NSUTF8StringEncoding];

[mailer addAttachmentData:myData mimeType:@"text/cvs" fileName:@"FileName"];

[self presentModalViewController:mailer animated:YES];

其中“文本”是一个字符串。

关于iphone - 如何在 iPhone sdk 的 MFMailComposer 中创建并附加 CSV 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10510142/

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