iOS Developer Library

Developer

MessageUI Framework Reference MFMessageComposeViewController Class Reference

Options
Deployment Target:

On This Page
Language:

MFMessageComposeViewController

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 NOfalse. 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.

Inheritance


Import Statement


Swift

import MessageUI

Objective-C

@import MessageUI;

Availability


Available in iOS 4.0 and later.
  • Returns a Boolean value indicating whether the current device is capable of sending text messages.

    Declaration

    Swift

    class func canSendText() -> Bool

    Objective-C

    + (BOOL)canSendText

    Return Value

    YEStrue if the device can send text messages or NOfalse 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.

    Import Statement

    Objective-C

    @import MessageUI;

    Swift

    import MessageUI

    Availability

    Available in iOS 4.0 and later.

  • Indicates whether or not messages can include attachments.

    Declaration

    Swift

    class func canSendAttachments() -> Bool

    Objective-C

    + (BOOL)canSendAttachments

    Return Value

    YEStrue if the device can send attachments in MMS or iMessage messages, or NOfalse otherwise.

    Import Statement

    Objective-C

    @import MessageUI;

    Swift

    import MessageUI

    Availability

    Available in iOS 7.0 and later.

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

    Declaration

    Swift

    class func canSendSubject() -> Bool

    Objective-C

    + (BOOL)canSendSubject

    Return Value

    YEStrue if the device can include subject lines in messages, or NOfalse otherwise.

    Import Statement

    Objective-C

    @import MessageUI;

    Swift

    import MessageUI

    Availability

    Available in iOS 7.0 and later.

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

    Declaration

    Swift

    class func isSupportedAttachmentUTI(_ uti: String!) -> Bool

    Objective-C

    + (BOOL)isSupportedAttachmentUTI:(NSString *)uti

    Parameters

    uti

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

    Return Value

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

    Import Statement

    Objective-C

    @import MessageUI;

    Swift

    import MessageUI

    Availability

    Available in iOS 7.0 and later.

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

    Declaration

    Swift

    var recipients: [AnyObject]!

    Objective-C

    @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.

    Import Statement

    Objective-C

    @import MessageUI;

    Swift

    import MessageUI

    Availability

    Available in iOS 4.0 and later.

  • subject subject Property

    The initial subject of the message.

    Declaration

    Swift

    var subject: String!

    Objective-C

    @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.

    Import Statement

    Objective-C

    @import MessageUI;

    Swift

    import MessageUI

    Availability

    Available in iOS 7.0 and later.

  • body body Property

    The initial content of the message.

    Declaration

    Swift

    var body: String!

    Objective-C

    @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.

    Import Statement

    Objective-C

    @import MessageUI;

    Swift

    import MessageUI

    Availability

    Available in iOS 4.0 and later.

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

    Declaration

    Swift

    var attachments: [AnyObject]! { get }

    Objective-C

    @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.

    Import Statement

    Objective-C

    @import MessageUI;

    Swift

    import MessageUI

    Availability

    Available in iOS 7.0 and later.

  • Attaches a specified file to the message.

    Declaration

    Swift

    func addAttachmentURL(_ attachmentURL: NSURL!, withAlternateFilename alternateFilename: String!) -> Bool

    Objective-C

    - (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

    YEStrue if the attachment at the specified URL was successfully added to the message, or NOfalse 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.

    Import Statement

    Objective-C

    @import MessageUI;

    Swift

    import MessageUI

    Availability

    Available in iOS 7.0 and later.

  • Attaches arbitrary content to the message.

    Declaration

    Swift

    func addAttachmentData(_ attachmentData: NSData!, typeIdentifier uti: String!, filename filename: String!) -> Bool

    Objective-C

    - (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

    YEStrue if the attachment data was successfully added to the message, or NOfalse 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.

    Import Statement

    Objective-C

    @import MessageUI;

    Swift

    import MessageUI

    Availability

    Available in iOS 7.0 and later.

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

    Declaration

    Swift

    func disableUserAttachments()

    Objective-C

    - (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.

    Import Statement

    Objective-C

    @import MessageUI;

    Swift

    import MessageUI

    Availability

    Available in iOS 7.0 and later.

Data Types

  • These constants describe the result of the message composition interface.

    Declaration

    Swift

    struct MessageComposeResult { init(_ value: UInt32) var value: UInt32 }

    Objective-C

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

    Constants

    • MessageComposeResultCancelled

      MessageComposeResultCancelled

      The user canceled the composition.

      Available in iOS 4.0 and later.

    • MessageComposeResultSent

      MessageComposeResultSent

      The user successfully queued or sent the message.

      Available in iOS 4.0 and later.

    • MessageComposeResultFailed

      MessageComposeResultFailed

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

      Available in iOS 4.0 and later.

    Import Statement

    Objective-C

    @import MessageUI;

    Swift

    import MessageUI

    Availability

    Available in iOS 4.0 and later.

  • A userInfo dictionary key for the MFMessageComposeViewControllerTextMessageAvailabilityDidChangeNotification notification.

    Declaration

    Swift

    let MFMessageComposeViewControllerTextMessageAvailabilityKey: NSString!

    Objective-C

    NSString *const MFMessageComposeViewControllerTextMessageAvailabilityKey;

    Constants

    • MFMessageComposeViewControllerTextMessageAvailabilityKey

      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.

  • The URL for the item attached to the message.

    Declaration

    Swift

    let MFMessageComposeViewControllerAttachmentURL: NSString!

    Objective-C

    NSString *const MFMessageComposeViewControllerAttachmentURL;

    Constants

    • MFMessageComposeViewControllerAttachmentURL

      MFMessageComposeViewControllerAttachmentURL

      The URL for the item attached to the message.

      Available in iOS 7.0 and later.

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

    Declaration

    Swift

    let MFMessageComposeViewControllerAttachmentAlternateFilename: NSString!

    Objective-C

    NSString *const MFMessageComposeViewControllerAttachmentAlternateFilename;

    Constants

    • MFMessageComposeViewControllerAttachmentAlternateFilename

      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.