Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leoiphonedev/eab2f6357ceba78b36003a9b9191468c to your computer and use it in GitHub Desktop.
Save leoiphonedev/eab2f6357ceba78b36003a9b9191468c to your computer and use it in GitHub Desktop.
COnformimng to MFMailComposeViewControllerDelegate so that we get notified on user certain action with mail composer view
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func sendEmail(_ sender: Any) {
if MFMailComposeViewController.canSendMail() {
let mailComposer = MFMailComposeViewController()
mailComposer.setSubject("Update about ios tutorials")
mailComposer.setMessageBody("What is the update about ios tutorials on youtube", isHTML: false)
mailComposer.setToRecipients(["abc@test.com"])
mailComposer.mailComposeDelegate = self
self.present(mailComposer, animated: true
, completion: nil)
} else {
print("Email is not configured in settings app or we are not able to send an email")
}
}
//MARK:- MailcomposerDelegate
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
switch result {
case .cancelled:
print("User cancelled")
break
case .saved:
print("Mail is saved by user")
break
case .sent:
print("Mail is sent successfully")
break
case .failed:
print("Sending mail is failed")
break
default:
break
}
controller.dismiss(animated: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment