无法将文本文件附加到电子邮件

时间:2014-09-25 20:54:45

标签: ios objective-c nsstring mfmailcomposer

我正在尝试生成一个文本文件并将其附加到电子邮件中。以前我使用此代码将文件附加到电子邮件中,并且已成功完成。

 NSString *recipient = @"myemail@me.com";
    NSArray *recipients = [NSArray arrayWithObjects:recipient, nil];

    MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
    mailViewController.mailComposeDelegate = self;
    [mailViewController setSubject:@"Invalid Scan"];
    [mailViewController setToRecipients:recipients];
    [mailViewController setMessageBody:@"" isHTML:NO];
    mailViewController.navigationBar.tintColor = [UIColor blackColor];

    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSString *textFilePath = [documentsDirectory stringByAppendingFormat:@"/textfile.txt"];
    NSData *myData = [NSData dataWithContentsOfFile:textFilePath];

  //  NSLog(@"my nsdata is %@",myData);

    [mailViewController addAttachmentData:myData
                                 mimeType:@"text/csv"
                                 fileName:textfile.txt];

但是当我尝试使用下面的语句时,文件没有附加:

     NSString *fileName_txtFile= @"textfile.txt";
NSString *textFilePath = [documentsDirectory stringByAppendingFormat:[NSString stringWithFormat:@"/%@",fileName_txtFile],nil];
    NSData *myData = [NSData dataWithContentsOfFile:textFilePath];



    [mailViewController addAttachmentData:myData
                                 mimeType:@"text/csv"
                                 fileName:fileName_txtFile];

我无法弄清楚,为什么文件没有附加到邮件上。你能帮我解决这个问题吗?

提前致谢!!

0 个答案:

没有答案