如何将csv文件作为电子邮件附件发送

时间:2018-02-21 01:30:24

标签: ios objective-c

我试图通过电子邮件发送csv文件并且我能够使用messageUI但是当我添加附件代码时,应用程序崩溃了。该文件位于文档目录中。错误和代码:

错误:

Assertion failure in -[MFMailComposeInternalViewController 
addAttachmentData:mimeType:fileName:], 
/BuildRoot/Library/Caches/com.apple.xbs/Sources/MessageUI/MessageUI-
3445.0.13/Mail/MFMailComposeInternalViewController.m:742
2018-02-20 18:25:54.649811-0700 oFiOSstoryboard[1385:174595] *** 
Terminating app due to uncaught exception 
'NSInternalInconsistencyException', reason: '-
[MFMailComposeInternalViewController 
addAttachmentData:mimeType:fileName:] attachment must not be nil.


- (IBAction)sendmail:(id)sender {
    if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
        mail.mailComposeDelegate = self;
        [mail setSubject:@"Sample Subject"];
        [mail setMessageBody:@"Here is some main text in the email!" isHTML:NO];
        [mail setToRecipients:@[@"testingEmail@example.com"]];
        NSData *myData = [NSData dataWithContentsOfFile:@"jorg.csv"];
        [mail addAttachmentData:myData
                                mimeType:@"text/csv"
                                fileName:@"jorg"];
        [self presentViewController:mail animated:YES completion:NULL];
    }
    else
    {
        NSLog(@"This device cannot send email");
    }
}

1 个答案:

答案 0 :(得分:2)

您的问题在于:

ActionTypes.ImBack

您声明文件位于Documents文件夹中,但此代码根本没有引用Documents文件夹。

你需要这样的东西来获取数据:

NSData *myData = [NSData dataWithContentsOfFile:@"jorg.csv"];