【问题标题】:Swift MFMailComposeViewController with XLS attachment, No attachment found带有 XLS 附件的 Swift MFMailComposeViewController,未找到附件
【发布时间】:2017-01-30 09:33:11
【问题描述】:

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

当我调试代码时,我看到它打印出“文件数据已加载。”,这意味着有数据来自沙箱(缓存)。不确定,这个 mime 类型是否正确"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)
   }

}

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

【问题讨论】:

    标签: ios swift excel swift3 mfmailcomposeviewcontroller


    【解决方案1】:

    下面的代码对我有用。

    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)
    }
    

    【讨论】:

    • 是的,这对我有用。你的文件扩展名是什么?
    • 仍然无法正常工作,当我检查我的沙箱时,它有 excel 文件,它还打印“文件数据已加载”,但我不明白为什么它没有将文件附加到电子邮件。
    • 你能不能至少在邮件中拿到正文?
    • 谢谢 :) 相同的代码可以在另一部手机上运行。我不知道为什么?我以前用过我的手机,我的手机设置了 2 封电子邮件。
    • 在设置->邮件->默认账户检查你的账户。但对我来说,它也适用于多个电子邮件设置。它使用默认帐户。
    猜你喜欢
    • 2012-10-25
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    相关资源
    最近更新 更多