In Android, I can launch an email client from my android application using its Intent mechanism. In ios, how can I launch its email client from my ios application using Swif
I found a great solution from Hacking in Swift by Paul Hudson. In Swift 3, Add import MessageUI
to the top of the file and make the class conform to MFMailComposeViewControllerDelegate
protocol.
func sendEmail() {
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(["example@example.com"])
mail.setMessageBody("You're so awesome!
", isHTML: true)
present(mail, animated: true)
} else {
// show failure alert
}
}
// MARK: MFMailComposeViewControllerDelegate Conformance
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true)
}