Contents

MFMessageComposeViewController

A standard view controller whose interface lets the user compose and send SMS or MMS messages.

Declaration

class MFMessageComposeViewController

Overview

Use an MFMessageComposeViewController object to display the standard message composition interface inside your app. Before presenting the interface, populate the fields with the set of initial recipients and the message you want to send. After presenting the interface, a person can edit your initial values before sending the message.

The composition interface doesn’t guarantee the delivery of your message; it only lets you construct the initial message and present it for a person’s approval. The person may opt to cancel the composition interface which discards the message and its contents. If the person opts to send the message, the Messages app takes on the responsibility of sending the message.

[Image]

An alternate way to compose SMS messages is to create and open a URL that uses the sms scheme. URLs of that type go directly to the Messages app, which uses your URL to configure the message. For information about the structure of sms URLs, see Apple URL Scheme Reference.

Checking the availability of the composition interface

Before presenting the message compose view controller, always call the canSendText() method to see if the person configured the current device to send messages. If the user’s device isn’t set up to send or receive messages, you can notify the user or disable the messaging features in your application. You shouldn’t attempt to use this interface if the canSendText() method returns false. If messaging is available, you can also use the canSendAttachments() and canSendSubject() methods to determine if those specific messaging features are available.

Configuring and displaying the composition interface

After verifying that message services are available, you can create and configure the message composition view controller and then present it like any other view controller. Use the methods of this class to specify the message’s recipients and the contents of the message. If attachments or a subject line are supported, you can set values for them as well. The sample code below shows how to configure the composition interface and present it modally. Always assign a delegate to the messageComposeDelegate property, because the delegate is responsible for dismissing the composition interface later. The delegate object must conform to the MFMessageComposeViewControllerDelegate protocol.

The message compose view controller isn’t dismissed automatically. When the user taps the buttons to send the message or cancel the interface, the message compose view controller calls the messageComposeViewController(_:didFinishWith:) method of its delegate. Your implementation of that method must dismiss the view controller explicitly, as shown in the sample code below. You can also use this method to check the result of the operation.

For more information on how to present and dismiss view controllers, see View Controller Programming Guide for iOS.

Detecting changes to the availability of messaging

Add an observer to the MFMessageComposeViewControllerTextMessageAvailabilityDidChange notification to get notified of changes to the messaging capabilities of the current device. The system delivers that notification to your observer when the status of messaging changes.

Topics

Responding to the view controller dismissal

Determining if message composition is available

Setting the initial message information

Managing attachments

Handling notifications

Configuring device validation