iOS Sharing — Send SMS

3-Steps to Send Out an SMS message

Myrick Chow
ITNEXT

--

SMS is probably the most reachable communication service among all the phone user. There is no need to install any 3rd party application to support sending out and receiving an SMS message. Apple has provided the default ‘Messages” app for iOS user.

iOS “Messages” App

Developers can implement the SMS delivery function to their own app by using the MessageUI framework. It is super easy and only requires 3 steps to setup. All the tasks are handled by the MFMessageComposeViewController and developer just needs to manually dismiss it once the user has completed his action.

Step-by-Step Setup

Step 1) Confirm if the device can send out text message

import MessageUI

// Confirm the device can send a text message through the system Message app
guard MFMessageComposeViewController.canSendText() else { return }

We need to confirm if the user has configured the iOS device to send out text message. The canSendText() returns false when it is running on an iPad with disabled iMessage. See below for the screenshot of the Settings page.

Settings at an iPad

Setp 2) Configure the MFMessageComposeViewController instance

// Construct the `MFMessageComposeViewController` instance
let mfMessageComposeViewController = MFMessageComposeViewController()

// Configure the fields of the interface.
mfMessageComposeViewController.recipients = ["+852 6123 4567"]
mfMessageComposeViewController.body = "Example message body"

// Present the view controller modally.
present(mfMessageComposeViewController, animated: true)

We need to then construct the MFMessageComposeViewController instance and configure the necessary fields of the SMS message.

  1. The recipient list — A list of phone number* that will receive the SMS message
    * Should better to include the country code, e.g “+852” for Hong Kong
  2. The message body — A simple plain text

Run the app and you can see the Message Composer popping up at the screen.

You can send out the SMS directly but it will NOT be dismissed right after the user sends out the SMS. We need to handle it at the MFMessageComposeViewControllerDelegate ourself.

Step 3) Implement the MFMessageComposeViewControllerDelegate


// Assigning the delegate to the `MFMessageComposeViewController` instance
mfMessageComposeViewController.messageComposeDelegate = self

extension SMSComposerViewController: MFMessageComposeViewControllerDelegate {

// This is the only callback from the Mail composer to notify the app that the user has carried out certain action
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
switch result {
case .cancelled:
print("The user has dismissed the Message composer.")

case .sent:
// The SMS / MMS send request is queued in the user's Messages app
// There is no guarantee that the message is actually sent out!!!
print("The user has sent out the message")

case .failed:
print("The user cannot send out the message")

}

controller.dismiss(animated: true)
}
}

The iOS does not dismiss the MFMessageComposeViewController by default. It is the developer’s responsibility to close it. We first need to implement the MFMessageComposeViewControllerDelegate and then call the dismiss() function at the end of the didFinishWith callback.

Trick — Does the message REALLY sent out when the `sent` result is returned?

Did you try to send out the message when there is NO SIGNAL CONNECTION, e.g. aeroplane mode? Oh NO! The didFinishWith callback of MFMessageComposeViewControllerDelegate actually returns a sent result!!!

What does that mean?

According to the below Apple documentation, the MessageUI framework will only send a queue request to the system. There is NO GUARANTEE if the the message is sent out to the recipients successfully.

Apple Official Documentation — MessageComposeResult.sent

The user successfully queued or sent the message.

Reference: Link

Advance features

Adding a subject field

// Check if the Message app supports to add a subject to the message
guard MFMessageComposeViewController.canSendSubject() else { return }

mfMessageComposeViewController.subject = "Example subject"

iOS Message supports a subject field with plain text. However, we need to check if the iOS device is configured to support a subject field first with the canSendSubject(). This subject function is disabled by default. We need to manually turn it on at the Settings first. See below for the path.

Adding an attachment


// Check if the Message app supports to add an attachment
guard MFMessageComposeViewController.canSendAttachments() else { return }

// Attach an image to the composed MMS
let attachmentImageData = UIImage(named: "btnAiSetting")!.pngData()!

// !!! Add the file extension name to the end of the file name to preview it at the Message Composer
let filenameWithExtension = "example_file_name.png"
mfMessageComposeViewController.addAttachmentData(attachmentImageData, typeIdentifier: "image/png", filename: filenameWithExtension)

The MessageUI framework also supports adding attachments to a MMS (Multimedia Messaging Service) message. Similar to the other fields, we have to also check if the device is configured to send out attachments with canSendAttachments()canSendAttachments() function.

Then, we need to extra the Data from out attachment source and pass it to the addAttachmentData() function. Remember to add the file extension name to the filename field, else the file cannot be previewed at the Message composer.

Conclusion

SMS is a very convenient way to delivery information among a group of user easily. Apple’s great framework MessageUI has provided us a simple to use API for implementing the SMS delivery function to any iOS app.

We need to construct the MFMessageComposeViewController with different fields, which should be checked one by one if the iOS device supports sending out that kind of information:

  1. canSendText() — Confirm the device can send out text message
  2. canSendSubject() — Ensure the subject field can be added
  3. canSendAttachments() — Confirm if attachments are allowed to add to the message

Finally, we need to understand that the iOS only returns the user’s action back to the app side. The sent result does not guarantee the SMS message is really delivered to the recipients.

Thank you for reading this article. Hope you find it interesting and useful! You are welcome to follow me and contact me through the following channels:

  1. Twitter@myrick_chow
  2. YouTube@myrickchow
  3. LinkedIn@Myrick Chow

--

--

Writer for