MFMessageComposeViewController Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/MessageUI.framework
Availability
Available in iOS 4.0 and later.
Companion guide
Declared in
MFMessageComposeViewController.h
Related sample code

Overview

The MFMessageComposeViewController class provides a standard system user interface for composing text messages. Use this class to configure the initial recipients and body of the message, if desired, and to configure a delegate object to respond to the final result of the user’s action—whether they chose to cancel or send the message. After configuring initial values, present the view controller modally using the presentModalViewController:animated: method. When done, dismiss it using the dismissModalViewControllerAnimated: method.

Before presenting a message composition view, call the canSendText class method to ensure that the user’s device is appropriately configured. Do not attempt to present a message composition view if the canSendText method returns NO. If neither iMessage nor SMS/MMS (Short Message Service/Multimedia Messaging Service) delivery is available, you can notify the user or simply disable the messaging features in your app.

You can register to be notified of changes to the availability of message sending by way of the MFMessageComposeViewControllerTextMessageAvailabilityDidChangeNotification notification.

Your delegate object is responsible for dismissing the message compose view controller in its messageComposeViewController:didFinishWithResult: method. For more information about implementing this method, see MFMessageComposeViewControllerDelegate Protocol Reference.

Tasks

Determining If Message Composition Is Available

Accessing the Delegate

Setting the Initial Message Information

Properties

attachments

Returns an array of dictionaries that each describe the properties of an attachment. (read-only)

@property(nonatomic, copy, readonly) NSArray *attachments
Discussion

Each attachment is described by an NSDictionary object in the attachments array. To retrieve the alternate file name for an attachment from its dictionary, use the MFMessageComposeViewControllerAttachmentAlternateFilename key.

Availability
  • Available in iOS 7.0 and later.
Declared In
MFMessageComposeViewController.h

body

The initial content of the message.

@property(nonatomic, copy) NSString *body
Discussion

If you want to provide initial content in the body of a message, do so before you display it. After the message is displayed you cannot change the value of this property.

Availability
  • Available in iOS 4.0 and later.
Related Sample Code
Declared In
MFMessageComposeViewController.h

messageComposeDelegate

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

@property(nonatomic, assign) id<MFMessageComposeViewControllerDelegate> messageComposeDelegate
Discussion

When the user taps a button to send or cancel the message, your delegate is notified and should respond by dismissing the message composition interface. For more information about implementing the methods of your delegate object, see MFMessageComposeViewControllerDelegate Protocol Reference.

Availability
  • Available in iOS 4.0 and later.
Related Sample Code
Declared In
MFMessageComposeViewController.h

recipients

An array of strings containing the initial recipients of the message.

@property(nonatomic, copy) NSArray *recipients
Discussion

If you want to provide an initial array of one or more recipients for a message, do so before you display it. After the message is displayed you cannot change the value of this property.

Each string in the array should contain the phone number of the intended recipient.

Availability
  • Available in iOS 4.0 and later.
Declared In
MFMessageComposeViewController.h

subject

The initial subject of the message.

@property(nonatomic, copy) NSString *subject
Discussion

If you want to provide an initial subject for a message, do so before you display it. After the message is displayed you cannot change the value of this property.

Availability
  • Available in iOS 7.0 and later.
Declared In
MFMessageComposeViewController.h

Class Methods

canSendAttachments

Indicates whether or not messages can include attachments.

+ (BOOL)canSendAttachments
Return Value

YES if the device can send attachments in MMS or iMessage messages, or NO otherwise.

Availability
  • Available in iOS 7.0 and later.
Declared In
MFMessageComposeViewController.h

canSendSubject

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

+ (BOOL)canSendSubject
Return Value

YES if the device can include subject lines in messages, or NO otherwise.

Availability
  • Available in iOS 7.0 and later.
Declared In
MFMessageComposeViewController.h

canSendText

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

+ (BOOL)canSendText
Return Value

YES if the device can send text messages or NO if it cannot.

Discussion

Always call this method before attempting to present the message compose view controller. A device may be unable to send messages if it does not support messaging or if it is not currently configured to send messages. This method applies only to the ability to send text messages via iMessage, SMS, and MMS.

To be notified of changes in the availability of sending text messages, register as an observer of the MFMessageComposeViewControllerTextMessageAvailabilityDidChangeNotification notification.

Availability
  • Available in iOS 4.0 and later.
Declared In
MFMessageComposeViewController.h

isSupportedAttachmentUTI:

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

+ (BOOL)isSupportedAttachmentUTI:(NSString *)uti
Parameters
uti

The UTI (Uniform Type Identifier) in question. See Uniform Type Identifiers Reference

Return Value

YES if a file with the specified UTI can be attached to the message, or NO otherwise.

Availability
  • Available in iOS 7.0 and later.
Declared In
MFMessageComposeViewController.h

Instance Methods

addAttachmentData:typeIdentifier:filename:

Attaches arbitrary content to the message.

- (BOOL)addAttachmentData:(NSData *)attachmentData typeIdentifier:(NSString *)uti filename:(NSString *)filename
Parameters
attachmentData

Content in the form of an NSData object to attach to the message. Must not be nil.

uti

A valid Uniform Type Identifier (UTI) appropriate for the attachment data. See Uniform Type Identifiers Reference. Must not be nil.

filename

The name to present to the user, in the message UI, for the data attachment.

Return Value

YES if the attachment data was successfully added to the message, or NO otherwise.

Discussion

This method is especially useful when the attachment you want to add to a message does not have a file system representation. This can be the case, for example, for programmatically composed audiovisual content.

Availability
  • Available in iOS 7.0 and later.
Declared In
MFMessageComposeViewController.h

addAttachmentURL:withAlternateFilename:

Attaches a specified file to the message.

- (BOOL)addAttachmentURL:(NSURL *)attachmentURL withAlternateFilename:(NSString *)alternateFilename
Parameters
attachmentURL

The file URL for the attachment. Must not be nil.

alternateFilename

If you supply a string here, the message UI uses it for the attachment. Use an alternate filename to better describe the attachment or to make the name more readable.

OK to use a nil value, in which case the attachment’s actual filename is displayed in the message UI.

Return Value

YES if the attachment at the specified URL was successfully added to the message, or NO otherwise.

Discussion

You can add zero or more attachments to a message before you display the message to the user. To access information about a message’s attachments, access the attachments property.

Availability
  • Available in iOS 7.0 and later.
Declared In
MFMessageComposeViewController.h

disableUserAttachments

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

- (void)disableUserAttachments
Discussion

In iOS 7.0 and later, call this method to disable the camera/attachment button in the message composition view. In an app linked against an older version of iOS, the camera/attachment button is not available in any case.

Availability
  • Available in iOS 7.0 and later.
Declared In
MFMessageComposeViewController.h

Constants

MessageComposeResult

These constants describe the result of the message composition interface.

enum MessageComposeResult {
   MessageComposeResultCancelled,
   MessageComposeResultSent,
   MessageComposeResultFailed
};
typedef enum MessageComposeResult MessageComposeResult;
Constants
MessageComposeResultCancelled

The user canceled the composition.

Available in iOS 4.0 and later.

Declared in MFMessageComposeViewController.h.

MessageComposeResultSent

The user successfully queued or sent the message.

Available in iOS 4.0 and later.

Declared in MFMessageComposeViewController.h.

MessageComposeResultFailed

The user’s attempt to save or send the message was unsuccessful.

Available in iOS 4.0 and later.

Declared in MFMessageComposeViewController.h.

Availability
  • Available in iOS 4.0 and later.
Declared In
MFMessageComposeViewController.h

MFMessageComposeViewControllerTextMessageAvailabilityKey

A userInfo dictionary key for the MFMessageComposeViewControllerTextMessageAvailabilityDidChangeNotification notification.

NSString *const MFMessageComposeViewControllerTextMessageAvailabilityKey;
Constants
MFMessageComposeViewControllerTextMessageAvailabilityKey

The value of this key is an NSNumber object containing a Boolean value. This value matches the result of the canSendText class method.

Available in iOS 5.0 and later.

Declared in MFMessageComposeViewController.h.

MFMessageComposeViewControllerAttachmentURL

The URL for the item attached to the message.

NSString *const MFMessageComposeViewControllerAttachmentURL;
Constants
MFMessageComposeViewControllerAttachmentURL

The URL for the item attached to the message.

Available in iOS 7.0 and later.

Declared in MFMessageComposeViewController.h.

MFMessageComposeViewControllerAttachmentAlternateFilename

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

NSString *const MFMessageComposeViewControllerAttachmentAlternateFilename;
Constants
MFMessageComposeViewControllerAttachmentAlternateFilename

Use this constant with the attachments property to retrieve the optional, more descriptive name for the attachment to a message.

Available in iOS 7.0 and later.

Declared in MFMessageComposeViewController.h.

Notifications

MFMessageComposeViewControllerTextMessageAvailabilityDidChangeNotification

Posted when the value returned by the canSendText class method has changed.

Upon receiving this notification, query its userInfo dictionary with the MFMessageComposeViewControllerTextMessageAvailabilityKey key. If the availability of text message sending has changed, your app should invalidate caches and update its user interface as appropriate.

Availability
Declared In
MFMessageComposeViewController.h