在 Swift 3 中,您可以像这样发送带有附件的邮件
@IBAction func emailLogs(_ sender: Any) {
let allPaths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectory = allPaths.first!
let pathForLog = documentsDirectory.appending("/application.log")
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self;
mail.setToRecipients(["recipient@email.com"])
mail.setSubject("Application Logs")
mail.setMessageBody("Please see attached", isHTML: true)
if let fileData = NSData(contentsOfFile: pathForLog) {
mail.addAttachmentData(fileData as Data, mimeType: "text/txt", fileName: "application.log")
}
self.present(mail, animated: true, completion: nil)
}
}
然后在结果上关闭作曲家控制器
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true, completion: nil)
}
确保订阅此委托
MFMailComposeViewControllerDelegate