我正在使用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"
我在设备中看到以下输出:(无附件和无电子邮件正文)
答案 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)
}