I am trying to set up an app with send email option.
I have this code:
import Foundation
import MessageUI
import UIKit
class emailClass: UIViewContr
Sending an Email is quit easy in Swift 5 you need to confirm and implement MFMailComposeViewControllerDelegate and check if we can send email on this device
Here is the tiny piece of code I was using for my task
import UIKit
import MessageUI
class ViewController: UIViewController, MFMailComposeViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
//MARK: IBAction Method for Button click
@IBAction func sendEmail(_ sender: Any) {
//TODO: You should chack if we can send email or not
if MFMailComposeViewController.canSendMail() {
let mail = MFMailComposeViewController()
mail.mailComposeDelegate = self
mail.setToRecipients(["you@yoursite.com"])
mail.setSubject("Email Subject Here")
mail.setMessageBody("You're so awesome!
", isHTML: true)
present(mail, animated: true)
} else {
print("Application is not able to send an email")
}
}
//MARK: MFMail Compose ViewController Delegate method
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
controller.dismiss(animated: true)
}
}
PS: Please don't forget you need to test this on real device