It’s all about delegation

A simple task of sending an email is also achieved in iOS through the delegation pattern. The basic concept is that we create an email view controller where the user can type the email details and set its delegate to which it will respond back with the results once the user has finished.

Procedure Breakdown

  • 1st step would be to design the mail form on the storyboard scene and add the subject,body fields as outlets and connect the send mail button as an a action
  • Now we need to import the MessageUI framework to send a mail from the view controller, the MessageUI framework en composes all the email related classes and protocols
  • Also make the view controller to conform to the MFMailComposeViewControllerDelegate, what we are actually doing is we will be presenting a view controller that will be responsible for sending the email and that controller will respond back to its delegate with information such as mail was sent successfully, mail was canceled, error in sending email etc, so we are essentially conforming to the delegate protocol as we want our view controller to be the delegate of that mail controller which we present eventually
  • Now the next step would be to create the mail view controller will the subject, recipients, body
  • Next we set the delegate of this newly created mail controller to be the view controller itself
  • Now we present the mail controller on the screen
  • Now we add the delegate methods to which the mail controller will respond back with information
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import Foundation
import UIKit
import MessageUI
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func sendEmailButtonTapped(sender: AnyObject) {
let mailComposeViewController = configuredMailComposeViewController()
if MFMailComposeViewController.canSendMail() {
self.presentViewController(mailComposeViewController, animated: true, completion: nil)
} else {
self.showSendMailErrorAlert()
}
}
func configuredMailComposeViewController() -> MFMailComposeViewController {
let mailComposeViewController = MFMailComposeViewController()
// Extremely important to set the --mailComposeDelegate-- property, NOT the --delegate-- property
mailComposeViewController.mailComposeDelegate = self
mailComposeViewController.setToRecipients(["[email protected]"])
mailComposeViewController.setSubject("Sending you an in-app e-mail...")
mailComposeViewController.setMessageBody("Sending e-mail in-app is not so bad!", isHTML: false)
return mailComposeViewController
}
func showSendMailErrorAlert() {
let sendMailErrorAlert = UIAlertView(title: "Could Not Send Email", message: "Your device could not send e-mail. Please check e-mail configuration and try again.", delegate: self, cancelButtonTitle: "OK")
sendMailErrorAlert.show()
}
}
//Conform to the MFMailComposeViewControllerDelegate protocol
extension ViewController: MFMailComposeViewControllerDelegate {
func mailComposeController(controller: MFMailComposeViewController!, didFinishWithResult result: MFMailComposeResult, error: NSError!) {
switch (result) {
case MFMailComposeResultSent:
print("The email message was queued in the user’s outbox. It is ready to send the next time the user connects to email.")
case MFMailComposeResultSaved:
print("The email message was saved in the user’s Drafts folder.")
case MFMailComposeResultCancelled:
print("The user canceled the operation. No email message was queued.")
case MFMailComposeResultFailed:
print("The email message was not saved or queued, possibly due to an error.")
default:
print("An error occurred when trying to compose this email")
}
controller.dismissViewControllerAnimated(true, completion: nil)
}
}