带有XLS附件的Swift MFMailComposeViewController,未找到附件

时间:2017-01-30 09:33:11

标签: ios swift excel swift3 mfmailcomposeviewcontroller

我正在使用XLS作为附件在Swift 3.0(Xcode 8.2.1)中使用#include <iostream> #include <algorithm> #include <iterator> #include <utility> #include <string> #include <vector> struct Thing { std::string val; }; int main() { Thing a_thing; std::vector<Thing> things; // take address auto first_thing = std::addressof(a_thing); // take address of "one past the end" - UB? auto last_thing = std::next(first_thing); // copying exactly one item, but is it UB? std::copy(first_thing, last_thing, std::back_inserter(things)); } 。我已经在Excel目录中保存了一个excel文件,并检索了相同的电子邮件附件。 (见下面的代码)

当我调试代码时,我看到它打印出来,“文件数据已加载。”,意味着有来自沙箱(缓存)的数据。不确定,这个mime类型是否正确MFMailComposeViewController

对我来说很奇怪!我没有看到邮件正文和附件。你能帮忙吗?

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

"application/vnd.ms-excel"

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

Device Screen shot

1 个答案:

答案 0 :(得分:2)

下面的代码适用于我。

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