Swift MFMailComposeViewController与XLS附件,找不到附件

我正在使用XLS作为附件在Swift 3.0(Xcode 8.2.1)中使用MFMailComposeViewController 。 我已经在caching目录中保存了一个excel文件,并检索相同的邮件附件。 (见下面的代码)

当我debugging代码时,我发现它打印的是“File data loaded”,意味着有数据来自沙箱(caching)。 不知道,这个MIMEtypes是正确的"application/vnd.ms-excel"

给我奇怪的! 我没有看到邮件正文和附件。 能否请你帮忙?

到目前为止,这里是我的代码:

 import MessageUI class ViewController:MFMailComposeViewControllerDelegate { func shareFileViaEmail() { if MFMailComposeViewController.canSendMail() { let mailComposerVC = MFMailComposeViewController() mailComposerVC.mailComposeDelegate = self let paths: [Any] = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true) let documentsDirectory: String = paths[0] as! String let dataPath: URL = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("record.xls").absoluteURL if let fileData = NSData(contentsOf: dataPath) { print("File data loaded.") mailComposerVC.addAttachmentData(fileData as Data, mimeType: "application/vnd.ms-excel", fileName: "Report") } mailComposerVC.setSubject("Report") mailComposerVC.setMessageBody("Message body", isHTML: false) self.present(mailComposerVC, animated: true, completion: nil) }else{ print("Your device could not send e-mail. Please check e-mail configuration and try again.") } } func mailComposeController(_ controller:MFMailComposeViewController, didFinishWith result:MFMailComposeResult, error:Error?) { self.dismiss(animated: false, completion: nil) } } 

我在设备中看到以下输出:(没有附件和没有电子邮件正文)

设备屏幕截图

下面的代码适合我。

 if( MFMailComposeViewController.canSendMail() ) { let mailComposer = MFMailComposeViewController() mailComposer.mailComposeDelegate = self //Set the subject and message of the email mailComposer.setSubject("swift") mailComposer.setMessageBody("Testing", isHTML: false) let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String let url = NSURL(fileURLWithPath: path) let filePath = url.appendingPathComponent("nameOfFileHere")?.path let fileManager = FileManager.default if fileManager.fileExists(atPath: filePath!) { if let fileData = NSData(contentsOfFile: filePath!) { print("File data loaded.") mailComposer.addAttachmentData(fileData as Data, mimeType: "application/vnd.ms-excel", fileName: "nameOfFileHere") } } else { print("FILE NOT AVAILABLE") } self.present(mailComposer, animated: true, completion: nil) }