import MessageUI
@IBAction func sendEmail(sender: UIButton) {
if( MFMailComposeViewController.canSendMail() ) {
let mailComposer = MFMailComposeViewController()
mailComposer.mailComposeDelegate = self
mailComposer.setSubject("Subject")
mailComposer.setMessageBody("body text", isHTML: false)
if let fileData = NSData(contentsOfFile: filePath) {
mailComposer.addAttachmentData(fileData, mimeType: "text/txt", fileName: "data")
}
self.presentViewController(mailComposer, animated: true, completion: nil)
}
}
http://kellyegan.net/sending-files-using-swift/
let strings = ["a","b"]
let joinedString = strings.joinWithSeparator("\n")
do {
try joinedString.writeToFile(path, atomically: true, encoding: NSUTF8StringEncoding)
} catch {
}
NSData , .
let filename = "testfile"
let strings = ["a","b"]
if(MFMailComposeViewController.canSendMail()){
let mailComposer = MFMailComposeViewController()
mailComposer.mailComposeDelegate = self
mailComposer.setToRecipients([mail])
mailComposer.setSubject("\(subject)" )
mailComposer.setMessageBody("\(messagebody)", isHTML: false)
let joinedString = strings.joinWithSeparator("\n")
print(joinedString)
if let data = (joinedString as NSString).dataUsingEncoding(NSUTF8StringEncoding){
mailComposer.addAttachmentData(data, mimeType: "text/plain", fileName: "test")
self.presentViewController(mailComposer, animated: true, completion: nil)
}
}
func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {
controller.dismissViewControllerAnimated(true, completion: nil)
}