Member-only story
Send Email from iOS app using Swift
MFMailComposeViewController
A standard interface for managing, editing, and sending an email message.
So let’s start with UI first. We will prepare the UI for the app.Here, We’ll take UITextFields and UIButton as below.
After placing the UIControls in UIViewContoller. Now its turn to set IBOutlet as follow:
@IBOutlet weak var textViewBody: UITextView!
@IBOutlet weak var textFieldSubject: UITextField!
@IBOutlet weak var textFieldTo: UITextField!
Import the library for MessageUI:
import MessageUI
Set delegate like
class ViewController: UIViewController,MFMailComposeViewControllerDelegate {
Write this code on button click action.
@IBAction func buttonHandlerSendEmail(_ sender: Any) {
let mailComposeViewController = configureMailComposer()
if MFMailComposeViewController.canSendMail(){
self.present(mailComposeViewController, animated: true, completion: nil)
}else{
print(“Can’t send email”)
}
}
Also write some methods like,
func configureMailComposer() -> MFMailComposeViewController{
let mailComposeVC = MFMailComposeViewController()…