Send SMS Messages Tutorial in iOS8 with Swift

With the MessageUI FrameWork we can add text messaging functionality in our app. In this tutorial we will send a text message using the MFMessageComposeViewController object. This tutorial is built with iOS 8.1 and Xcode 6.1

Open Xcode and create a new Single View Application. For product name, use iOS8SwiftSendSMSTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

Go to the Storyboard. Drag a Button to the main view. Select the button and give it a title of "Send SMS". Change the background of the main View to light-gray. Select the Button and choose the "Resolve Auto Layout Button". (3rd button in the bottom-right corner) and select "Add Missing Constraints".

The Storyboard should look like this. 

Select the Assistant Editor and make sure the ViewController.swift file is visible. Ctrl and drag from the Button to the View Controller class and create the following Action.

Our View Controller must be able to respond to updates from a message compositon, so it must conform to the MFMessageComposeViewControllerDelegate protocol. In ViewController.swift change the class declaration to

class ViewController: UIViewController, MFMessageComposeViewControllerDelegate {

Next, implement the sendMessage method

@IBAction func sendMessage(sender: AnyObject) {
    var messageVC = MFMessageComposeViewController()
    messageVC.body = "Enter a message";
    messageVC.recipients = ["Enter tel-nr"]
    messageVC.messageComposeDelegate = self;
    self.presentViewController(messageVC, animated: false, completion: nil)

We initialize a MFMessageComposeViewController and fill in the body and recipiens. Fill in your own values. Then the View Controller is presentented. The user can edit the body and change the recipients if they like, and press the send button. To conform the view controller to the MFMessageComposeViewControllerDelegate protocol, the messageComposeViewController:didFinishWithResult method is required.

func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {
    switch (result.value) {
    case MessageComposeResultCancelled.value:
      println("Message was cancelled")
      self.dismissViewControllerAnimated(true, completion: nil)
    case MessageComposeResultFailed.value:
      println("Message failed")
      self.dismissViewControllerAnimated(true, completion: nil)
    case MessageComposeResultSent.value:
      println("Message was sent")
     self.dismissViewControllerAnimated(true, completion: nil)

We check if the messaging has succeeded or not and we dismiss our view controller. Because the Simulator isn't able to send SMS messages, this project should be run on a device. Build and Run the project, enter some text and send the SMS message


You can download the source code of the iOS8SwiftSendSMSTutorial at the ioscreator repository on github.