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
SWIFT 3: The openEmail function will try to use the iOS Mail app if it is available (user has atleast one email account setup). Otherwise it will use the mailto: url (see full example at the bottom of my reply) to launch a mail client.
import MessageUI
// Make your view controller conform to MFMailComposeViewControllerDelegate
class Foo: UIViewController, MFMailComposeViewControllerDelegate {
func openEmail(_ emailAddress: String) {
// If user has not setup any email account in the iOS Mail app
if !MFMailComposeViewController.canSendMail() {
print("Mail services are not available")
let url = URL(string: "mailto:" + emailAddress)
UIApplication.shared.openURL(url!)
return
}
// Use the iOS Mail app
let composeVC = MFMailComposeViewController()
composeVC.mailComposeDelegate = self
composeVC.setToRecipients([emailAddress])
composeVC.setSubject("")
composeVC.setMessageBody("", isHTML: false)
// Present the view controller modally.
self.present(composeVC, animated: true, completion: nil)
}
// MARK: MailComposeViewControllerDelegate
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
// Dismiss the mail compose view controller.
controller.dismiss(animated: true, completion: nil)
}
}
An all out mailto example with subject, body, and cc:
"mailto:me@gmail.com?subject=Hey whats up man!&body=This is from a great example I found online.&cc=someoneelse@yahooo.com&bcc=whostillusesthis@hotmail.com"