74

Send Email iOS Tutorial

 5 years ago
source link: https://www.tuicool.com/articles/hit/mqiyMbm
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

It is possible to compose and send an email from inside an app using the MFMailComposeViewController class. In this tutorial an email is composed with a body and a subject. This tutorial is made with Xcode 10 and built for iOS 12.

Open Xcode and create a new Single View App.

viM3ea3.png!web

For product name, use IOS12SendEmailTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and choose Next.

FzMzUn3.png!web

Go to the Storyboard and drag a Text Field to the view. Select the Text Field and go to the Attribute Inspector, in the Text Field section change the placeholder text to "Subject". Next, drag a Text View below the Text Field. Select the Text View and go to the Attribute Inspector. In the Text View section delete the template text.Finally, add a Button to the view and place it below the Text View. Select it and give it s title of "Send Email" . Also, change the background color of the main view to light-gray. The Storyboard will look like this

3uAZbiN.png!web

Select the "Resolve Auto Layout Issues" button at the bottom-right(3rd button) and select Add Missing Constraints.

iQbyIfI.png!web

Select the Assistant Editor and make sure the ViewController.swift is visible. Ctrl and drag from the Text Field to the ViewController class  and create the following Outlet.

qYZFVzQ.png!web

Ctrl and drag from the Text View to the ViewController class  and create the following Outlet.

6VvIvaq.png!web

Ctrl and drag from the Button to the ViewController class  and create the following Action.

UNr2Av7.png!web

Go to the ViewController.swift file, first we need to import the MessageUI framework.

import MessageUI

The View Controller must conform to a couple of delegates change the class definition line into

class ViewController: UIViewController, MFMailComposeViewControllerDelegate, UITextFieldDelegate, UITextViewDelegate {

Change the viewDidLoad method to

override func viewDidLoad() {
    super.viewDidLoad()
    
    subject.delegate = self
    body.delegate = self
}

The delegates of the TextField and TextView now points to the View Controller. Next, implement the sendMail method

@IBAction func sendMail(_ sender: Any) {
    let picker = MFMailComposeViewController()
    picker.mailComposeDelegate = self
        
    if let subjectText = subject.text {
        picker.setSubject(subjectText)
    }
    picker.setMessageBody(body.text, isHTML: true)
        
    present(picker, animated: true, completion: nil)
}

The MFMailComposeViewController class provides a standard interface that manages the editing and sending an email message. The View Controller is presented including the subject and body's text. Finally we need to implement some delegate methods.

// MFMailComposeViewControllerDelegate

// 1
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
     dismiss(animated: true, completion: nil)
}

// UITextFieldDelegate
    
// 2
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()
        
    return true
}
    
// UITextViewDelegate
    
// 3
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    body.text = textView.text
        
    if text == "\n" {
        textView.resignFirstResponder()
            
        return false
    }
        
    return true
}
  1. Tells the delegate that the user wants to dismiss the mail composition view.

  2. In the textFieldShouldReturn method we resign The FirstResponder so the control is given back to the ViewController.

  3. When the enter key is pressed. the keyboard will hide.

Build and Run, the project. Enter some subject and body text.

63aaqay.png!web

Press the Send Email button. The MFMailCompose ViewController will be displayed containing the subject and body's text.

6F3Avyz.png!web

You can download the source code of the IOS12SendEmailTutorial at the ioscreator repository on  Github .


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK