Home » Sending Email using the iOS application

Sending Email using the iOS application

by Online Tutorials Library

Sending Email using the iOS application

While developing mobile applications, there are requirements where we have to allow users to send emails using our iOS app. In this article, we will discuss how we can send emails using the iOS application. Allowing users to send emails using our application can be very useful for customer support. We can also take direct feedback from the users if we provide an email send button in the application.

Here, we will build an application that provides the button to the users to send emails.

Let’s create the XCode project as MailerProject, as shown below.

Sending Email using the iOS application

Let’s import the MessageUI framework, which will be used to present email composer in the app.

Now, let’s set up a button that will be used by the users to send the mail. The following code will set up the button placed in the center of the ViewController.

We will also add a target function for the button called when the button gets tapped by the user.

We need to add the MFMailComposeViewControllerDelegate method, which calls the mail ViewController delegate directly from the app. It allows us to show email composer, which is an actual UI that will be used by the user to send the email.

The delegate method also contains the MFMailComposeResult object, which is the Enum that includes all the scenarios the user can come across while sending an email.

Now, we will code the showMailUI() to present the email composer. In the following code, we set the mail attributes like recipient, the subject, and the body. We can also add the attachments to the email using the addAttachmentData() method.

We must notice that the MFMailComposeViewController is linked to Apple’s mail app. To run the application and send an email, we need to run the app on the physical device. We should see the mail composer on the device with an email ready to be sent. The ViewController.swift contains the following code.


You may also like