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