Class

MFMessageComposeViewController

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

Declaration

class MFMessageComposeViewController : UINavigationController

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, the user can edit your initial values before sending the message.

The composition interface does not guarantee the delivery of your message; it only lets you construct the initial message and present it for user approval. The user may opt to cancel the composition interface, in which case the message and its contents are discarded. If the user opts to send the message, the Messages app takes on the responsibility of sending the message.

Figure 1

The message composition interface

Screenshot of the New Message screen, with a phone number in the To field and a short sentence in the composition text field.

An alternate way to compose SMS messages is to create and open a URL that uses the sms scheme. URLs of that type are directed 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 the canSendText() method to see if the current device is configured to send messages. If the user’s device is not set up to send or receive messages, you can notify the user or simply disable the messaging features in your application. You should not 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.

Listing 1

Checking the availability of message services

if !MFMessageComposeViewController.canSendText() {
    print("SMS services are not 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 you would 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. Listing 2 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.

Listing 2

Configuring and presenting the composition interface

let composeVC = MFMessageComposeViewController()
composeVC.messageComposeDelegate = self
 
// Configure the fields of the interface.
composeVC.recipients = ["4085551212"]
composeVC.body = "Hello from California!"
 
// Present the view controller modally.
self.presentViewController(composeVC, animated: true, completion: nil)

The message compose view controller is not 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 Listing 3. You can also use this method to check the result of the operation.

Listing 3

Dismissing the message compose view controller

func messageComposeViewController(controller: MFMessageComposeViewController,
                                  didFinishWithResult result: MessageComposeResult) {
    // Check the result or perform other tasks.
    
    // Dismiss the message compose view controller.
    controller.dismissViewControllerAnimated(true, completion: nil)}

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

To be notified of changes to the messaging capabilities of the current device, add an observer to the MFMessageComposeViewControllerTextMessageAvailabilityDidChange notification. The system delivers that notification to your observer when the status of messaging changes.

Topics

Responding to the View Controller Dismissal

var messageComposeDelegate: MFMessageComposeViewControllerDelegate?

The delegate to which message-related notifications should be sent.

protocol MFMessageComposeViewControllerDelegate

An interface for responding to user interactions with a message compose view controller.

Determining If Message Composition Is Available

class func canSendText() -> Bool

Returns a Boolean value that indicates whether the current device is capable of sending text messages.

class func canSendAttachments() -> Bool

Indicates whether or not messages can include attachments.

class func canSendSubject() -> Bool

Indicates whether or not messages can include subject lines, according to the user’s configuration in Settings.

class func isSupportedAttachmentUTI(String) -> Bool

Indicates whether or not the message can accept a file, with the specified UTI, as an attachment.

Setting the Initial Message Information

var recipients: [String]?

An array of strings that contains the initial recipients of the message.

var subject: String?

The initial subject of the message.

var body: String?

The initial content of the message.

var message: MSMessage?

A message object from your iMessage app extension.

Managing Attachments

func disableUserAttachments()

Disables the camera/attachment button in the message composition view.

var attachments: [[AnyHashable : Any]]?

Returns an array of dictionaries that describe the properties of an attachment.

let MFMessageComposeViewControllerAttachmentURL: String

The URL for the item that is attached to the message.

let MFMessageComposeViewControllerAttachmentAlternateFilename: String

The key for the alternate filename for the file-based item attached to the message.

Handling Notifications

let MFMessageComposeViewControllerTextMessageAvailabilityKey: String

The value of this key is a number object that contains a Boolean value.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software