自动附加电子邮件iOS

时间:2014-10-21 11:21:36

标签: ios email csv

我一直在构建一个调查应用程序,用户只需输入信息并将其保存到csv文件中。我现在处于需要将应用程序中的csv文件附加到电子邮件地址的阶段,因此当用户点击提交时,它会自动将电子邮件中的csv文件作为附件发送,我的代码如下:

- (IBAction)send:(id)sender {

    NSString *savedFilePath = @"../contact.csv";
    NSData *csvData = [NSData dataWithContentsOfFile:savedFilePath];

    MFMailComposeViewController *mailcomposer = [[MFMailComposeViewController alloc] init];
    [mailcomposer setMailComposeDelegate:self];
    [mailcomposer addAttachmentData:csvData mimeType:@"text/csv" fileName:@"contact.csv"];
    [mailcomposer setToRecipients:@[@"email@here.co.uk"]];
    [mailcomposer setSubject:self.subject.text];
    [mailcomposer setMessageBody:self.message.text isHTML:NO];
    [self presentModalViewController:mailcomposer animated:YES];
}

-(void) mailComposeController: (MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    [self dismissModalViewControllerAnimated:YES];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

然而这并没有附加csv文件......我觉得它可能与行有关

 NSString *savedFilePath = @"../contact.csv";

但是我不确定。如果有人可以提供帮助,请告诉我......我已经达到了这一点的突破点。

1 个答案:

答案 0 :(得分:1)

问题可能在于您使用相对路径加载NSData

NSString *savedFilePath = @"../contact.csv";
NSData *csvData = [NSData dataWithContentsOfFile:savedFilePath];

尝试创建文件的完整路径:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedFilePath = [documentsDirectory stringByAppendingPathComponent:@"result‌s.csv"];
NSData *csvData = [NSData dataWithContentsOfFile:savedFilePath];

这里我假设contact.csv在文档目录中。