iOS csv附件到电子邮件

时间:2014-11-17 21:07:39

标签: ios email csv attachment mfmailcomposeviewcontroller

我可以使用我的iOS应用程序成功添加附件到电子邮件,但仅当我硬编码文件名时,否则附件中没有数据。

这有效:

(IBAction)sendEmail:(id)sender
{
transectNameString = [[NSUserDefaults standardUserDefaults] objectForKey:@"dipstransect"];
transectFileExt = [NSString stringWithFormat:@".csv"];
NSString *string1 = transectNameString;
NSString *string2 = @" Drops";
NSString *string3 = transectFileExt;
NSString *tempstring1 = [string1 stringByAppendingString:string2];
setTransectName = [tempstring1 stringByAppendingString:string3];

MFMailComposeViewController *mailComposer;
NSArray *emailAddresses;

mailComposer = [MFMailComposeViewController new];
emailAddresses = @[];
mailComposer.mailComposeDelegate = self;
[mailComposer setToRecipients:emailAddresses];

filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
fileName = @"Paynes Drops.csv";
fileAtPath = [filePath stringByAppendingPathComponent:fileName];
NSData *fileData = [NSData dataWithContentsOfFile:fileAtPath];

[mailComposer addAttachmentData:fileData mimeType:@"text/csv" fileName:fileName];

[self presentViewController:mailComposer animated:YES completion:nil];

}

这不起作用:

(IBAction)sendEmail:(id)sender
{
transectNameString = [[NSUserDefaults standardUserDefaults] objectForKey:@"dipstransect"];
transectFileExt = [NSString stringWithFormat:@".csv"];
NSString *string1 = transectNameString;
NSString *string2 = @" Drops";
NSString *string3 = transectFileExt;
NSString *tempstring1 = [string1 stringByAppendingString:string2];
setTransectName = [tempstring1 stringByAppendingString:string3];

MFMailComposeViewController *mailComposer;
NSArray *emailAddresses;

mailComposer = [MFMailComposeViewController new];
emailAddresses = @[];
mailComposer.mailComposeDelegate = self;
[mailComposer setToRecipients:emailAddresses];

filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
fileName = setTransectName;
fileAtPath = [filePath stringByAppendingPathComponent:fileName];
NSData *fileData = [NSData dataWithContentsOfFile:fileAtPath];

[mailComposer addAttachmentData:fileData mimeType:@"text/csv" fileName:fileName];

[self presentViewController:mailComposer animated:YES completion:nil];

}

导致问题的fileName是什么?

1 个答案:

答案 0 :(得分:0)

我将以下行移至ViewDidLoad语句并且它有效。

  • (无效)viewDidLoad中 {

    transectNameString = [[NSUserDefaults standardUserDefaults] objectForKey:@“dipstransect”];

    [super viewDidLoad]; //加载视图后进行任何其他设置。 }

  • (IBAction为)sendEmail:(ID)发送方 {

    MFMailComposeViewController * mailComposer; NSArray * emailAddresses;

    mailComposer = [MFMailComposeViewController new]; emailAddresses = @ []; mailComposer.mailComposeDelegate = self; [mailComposer setToRecipients:emailAddresses];

    filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];

    transectFileExt = [NSString stringWithFormat:@“。csv”]; NSString * string2 = @“Drops”;

    NSString * tempString1 = [transectNameString stringByAppendingString:string2]; NSString * tempString2 = [tempString1 stringByAppendingString:transectFileExt];

    fileName = tempString2; fileAtPath = [filePath stringByAppendingPathComponent:fileName]; NSData * fileData = [NSData dataWithContentsOfFile:fileAtPath];

    [mailComposer addAttachmentData:fileData mimeType:@“text / csv”fileName:fileName];

    transectFile2Ext = [NSString stringWithFormat:@“。txt”]; NSString * string4 = @“Drops Notes”;

    NSString * tempString3 = [transectNameString stringByAppendingString:string4]; NSString * tempString4 = [tempString3 stringByAppendingString:transectFile2Ext];

    file2Name = tempString4; file2AtPath = [filePath stringByAppendingPathComponent:file2Name]; NSData * file2Data = [NSData dataWithContentsOfFile:file2AtPath];

    [mailComposer addAttachmentData:file2Data mimeType:@“text / txt”fileName:file2Name];

    [self presentViewController:mailComposer animated:YES completion:nil];

}