How can I launch an email client on ios using Swfit

前端 未结 4 635
误落风尘
误落风尘 2021-01-02 06:04

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

4条回答
  •  独厮守ぢ
    2021-01-02 06:30

    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) }

提交回复
热议问题