Skip to content

Instantly share code, notes, and snippets.

@leodabus
Created April 23, 2017 23:32
Show Gist options
  • Save leodabus/587a5146c1544020fae00260126749ea to your computer and use it in GitHub Desktop.
Save leodabus/587a5146c1544020fae00260126749ea to your computer and use it in GitHub Desktop.
import UIKit
import MessageUI
class MailComposeViewController: MFMailComposeViewController, MFMailComposeViewControllerDelegate {
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true) {
switch result {
case .cancelled:
print ("Canceled")
case .saved:
print ("Saved")
case .sent:
print ("Sent")
case .failed:
print ("Error:", error ?? "nil")
}
}
}
convenience init?(to: [String], subject: String, body: String) {
guard MFMailComposeViewController.canSendMail() else { return nil }
self.init()
mailComposeDelegate = self
setToRecipients(to)
setSubject(subject)
setMessageBody(body, isHTML: true)
}
}
@leodabus
Copy link
Author

to call it from your IBAction

@IBAction func ok(_ sender: UIButton) {

    if let mailComposer = MailComposeViewController(to: ["[email protected]"], subject: "Testing", body: "Testy test testing") {
        present(mailComposer, animated: true, completion: nil)
    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment