iOS Send Email App

  Total Views : 60
  Comments : 0
 
  Keywords : iOS send email from swift applications with example, iOS send mail with attachments in swift app with example, Use ios mfmailcomposeviewcontroller to send email in swift with example

Here we will learn how to create iOS app to send email in swift with example and how to use iOS MFMailComposeViewController to send email in swift with example.

iOS Send Email App

In iOS we can easily send email by using MFMailComposeViewController reference in our swift applications.

 

If we use iOS MFMailComposeViewController component in our application automatically it provides a standard email interface in our app with fields to recipients, subject, body, etc to send mail like as shown below.

 

ios send email app sample example

 

Initially from our app we need to send recipients email, subject, body and attachments if required then call MFMailComposeViewController reference to open email interface with pre populated values like as shown above. After opening the email interface user can edit the initial details before sending an email.

 

The email interface will provide an options like Cancel and Send in case if user decided not to send mail then he can click on cancel button the email content will discard automatically. If user want to send message, the message will add to users Mail app outbox. The Mail app is responsible for sending the message.

 

Now we will see how to send email in iOS swift applications with example.

Create iOS Send Email App in Swift

To create new project in iOS open Xcode from /Applications folder directory. Once we open Xcode the welcome window will open like as shown below. In welcome window click on the second option “Create a new Xcode Project” or choose File --> New --> Project.

 

Xcode application to create ios project

 

After selecting “Create a new Xcode project” a new window will open in that we need to choose template.

 

The new Xcode window will contain several built-in app templates to implement common type of iOS apps like page based apps, tab-based apps, games, table-view apps, etc. These templates are having pre-configured interface and source code files. 

 

For this iOS Send Email example, we will use most basic template “Single View Application”. To select this one, Go to the iOS section in left side --> select Application --> In main area of dialog select “Single View Application” and then click on next button like as shown below.

 

Select single view application from ios xcode templates

 

After click Next we will get window like as shown below, in this we need to mention project name and other details for our application.

 

Product Name: “Send Email in iOS”

 

The name whatever we enter in Product Name section will be used for the project and app.

 

Organization Name: “Tutlane”

 

You can enter the name of your organization or your own name or you can leave it as blank.

 

Organization Identifier: “com.developersociety”

 

Enter your organization identifier in case if you don't have any organization identifier enter com.example.

 

Bundle Identifier: This value will generate automatically based on the values we entered in Product Name and Organization Identifier.

 

Language: “Swift”

 

Select language type as “Swift” because we are going to develop applications using swift.

 

Devices: “Universal”

 

Choose Devices options as Universal it means that one application is for all apple devices in case if you have any specific requirement to run app only for iPad then you can choose the iPad option to make your application restricted to run only on iPad devices.

 

Use Core Data: Unselected

 

This option is used for database operations. In case if you have any database related operations in your application select this option otherwise unselect the option.

 

Include Unit Tests: Unselected

 

In case if you need unit tests for your application then select this option otherwise unselect it.

 

Include UI Tests: Unselected

 

In case if you need UI tests for your application then select this option otherwise unselect it.

 

Once you finished entering all the options then click on Next button like as shown below.

 

ios create new email app in swift using xcode editor

 

Once we click on Next button new dialog will open in that we need to select the location to save our project. Once you select the location to save project then click on Create button like as shown below.

 

Give path to save new ios application in xcode

 

After click on Create button the Xcode will create and open a new project. In our project Main.storyboard and ViewController.swift are the main files which we used to design app user interface and to maintain source code. 

 

Main.storyboard - Its visual interface editor and we will use this file to design our app user interface 

 

ViewController.swift - It contains source code of our application and we use this file to write any code related to our app.

 

Now in project select Main.storyboard file the Xcode will open visual interface editor like as shown below.

 

ios send email app storyboard file in xcode

 

Now select ViewController.swift file in your project that view will be like as shown below.

 

ios send email app viewcontroller file in xcode

 

Now click on Project then go to “Build Phase” and click on Plus (+) button and search for MessageUI and add “MessageUI.framework” into your project like as shown below.

 

ios send email app add message ui framework in xcode

Add iOS UI Controls to View in Swift

Now we will add controls to our application for that open Object Library. The Object Library will appear at the bottom of Xcode in right side. In case if you don't find Object library, click on the button which is at the third position from the left in the library selector bar like as shown below. (Alternatively you can choose View --> Utilities --> Show Object Library.)

 

Object Library in Xcode Application to Search for UI Controls

 

As we discussed our user interface will be in Main.storyboard file so open Main.storyboard file. Now in Object library search for Button in Filter field then drag and drop Button into Main.storyboard ViewController and change button name as Contact Us like as shown below

 

ios send email app add controls to viewcontroller in swift

Connect iOS UI Controls to Code in Swift

Now we will make connection between controls and ViewController.Swift code for that click on assistant button (overlap circle) in Xcode toolbar right side corner like as shown below

 

Assistant Editor in iOS Xcode to Mapp Controls with Code

 

To map the controls, press Ctrl button in keyboard and drag the View and button from view controller and drop into ViewController.swift file like as shown below

 

ios send email app map controls to viewcontroller.swift file in xcode

 

Once we done all the settings we need to write custom code to send email on button click. Our ViewController.swift file should contain code like as shown below

 

//  ViewController.swift

//  Send Email in iOS

//  Created by Tutlane on 10/08/2016.

//  Copyright © 2016 Tutlane. All rights reserved.

 

import UIKit

import MessageUI

class ViewController: UIViewControllerMFMailComposeViewControllerDelegateUITextFieldDelegateUITextViewDelegate

{

@IBAction func SendEmail(sender: AnyObject) {

let picker = MFMailComposeViewController()

picker.mailComposeDelegate = self

picker.setToRecipients(["support@tutlane.com"])

picker.setMessageBody("<b>Welcome To Tutlane.Com</b>", isHTML: true)

picker.setSubject("Information")

presentViewController(picker, animated: true, completion: nil)

}

func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) {

dismissViewControllerAnimated(true, completion: nil)

}

override func viewDidLoad() {

super.viewDidLoad()

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

Now we will run and check the output of application. To run application, select the required simulator (Here we selected iPhone 6s Plus) and click on Play button, located at the top-left corner of the Xcode toolbar like as shown below.

 

Run ios send email app using simulator in xcode

Output of iOS Send Email App in Swift

Following is the result of iOS send email app in swift. Now click on Contact Us button automatically the email template interface will open with pre populated values like as shown below. Here if you click on “Send” button your “Mail App” is responsible to send mail

 

ios send email app example result or output

 

This is how we can use iOS MFMailComposeViewController to send email from our swift application based on our requirement.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User