Im working on a simple swift app where the user inputs an email address and presses a button which opens the mail app, with the entered address in the address bar. I know ho
In the view controller from where you want your mail-app to open on the tap.
Put this function inside your Controller.
func showMailComposer(){
guard MFMailComposeViewController.canSendMail() else {
return
}
let composer = MFMailComposeViewController()
composer.mailComposeDelegate = self
composer.setToRecipients(["abc@gmail.com"]) // email id of the recipient
composer.setSubject("testing!!!")
composer.setMessageBody("this is a test mail.", isHTML: false)
present(composer, animated: true, completion: nil)
}
Extend your View Controller and conform to the MFMailComposeViewControllerDelegate.
Put this method and handle the failure, sending of your mails.
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
if let _ = error {
controller.dismiss(animated: true, completion: nil)
return
}
controller.dismiss(animated: true, completion: nil)
}