0

这是我的代码:

NSArray *recipient = [NSArray arrayWithObjects:@"testemail@office.co.uk",nil];

NSString *fileName = @"SurveyResults.txt";
NSString *homeDir = NSHomeDirectory();
NSString *fullPath = [homeDir stringByAppendingPathComponent:fileName];

[super viewDidLoad];
if([MFMailComposeViewController canSendMail]) {
    MFMailComposeViewController *mailViewController = [[MFMailComposeViewController alloc] init];
    mailViewController.mailComposeDelegate = self;
    [mailViewController setSubject:[NSString stringWithFormat:@"Place Holder Subject"]];
    [mailViewController setMessageBody:@"See Attached" isHTML:NO];
    [mailViewController setToRecipients:recipient];
    [mailViewController addAttachmentData:[NSData dataWithContentsOfFile:fullPath] mimeType:@"text/txt" fileName:fileName];

    [self presentModalViewController:mailViewController animated:YES];
} else
    NSLog(@"Device cannot send email at this time. Please check you are connected to the internet and try again");

问题是,当 MailComposer 启动时,它在“查看附件”下有一个小 txt 文件图像,下面有一个名为 SurveyResults 的名称,但是,当我实际发送电子邮件时,txt 文件没有附加,有人可以解释一下吗?

4

1 回答 1

0

尝试以下,为我工作。问题是,填充数据的路径必须是类型NSURL,而不仅仅是字符串路径。

    NSURL *fileURL = [[NSURL alloc]initFileURLWithPath:string_path_of_the_file];
    NSData *data = [[NSData alloc]initWithContentsOfURL:fileURL];

    [mailViewController addAttachmentData:data mimeType:@"text/plain" fileName:fileNameString];   
     //mimeType needs to be changed accordingly
于 2012-12-03T09:30:12.083 回答