In iOS 4.0 and later, you can send text messages from within your application. This feature is strictly for sending messages. Incoming SMS messages go to the built-in Messages app.

To use the SMS composition interface, you must add MessageUI.framework to your Xcode project and link against it in any relevant targets. To access the classes and headers of the framework, include an #import <MessageUI/MessageUI.h> statement at the top of any relevant source files. For information on how to add frameworks to your project, see “Files in Projects” in Xcode Project Management Guide.

To provide the standard user interface for composing an SMS (Short Message Service) message, use the <a href="http://developer.apple.com/library/ios/documentation/MessageUI/Reference/MFMessageComposeViewController_class/Reference/Reference.html#//apple_ref/occ/cl/MFMessageComposeViewController" target="_self">MFMessageComposeViewController</a> class. Create an instance of this class and assign it a delegate object. The delegate must conform to the <a href="http://developer.apple.com/library/ios/documentation/MessageUI/Reference/MFMessageComposeViewControllerDelegate_protocol/Reference/Reference.html#//apple_ref/occ/intf/MFMessageComposeViewControllerDelegate" target="_self">MFMessageComposeViewControllerDelegate</a>protocol.

Before presenting the composition interface to the user, you can configure initial recipients and message content. With setup complete, call the UIViewController <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/presentModalViewController:animated:" target="_self">presentModalViewController:animated:</a> method to present the SMS message composition view controller modally.

While the interface is visible, the user can edit the recipients list and message content. The user then sends the message, or cancels it, by tapping the appropriate control in the interface.

If the user requests that the message be sent, the system queues it for delivery and invokes the delegate object’s <a href="http://developer.apple.com/library/ios/documentation/MessageUI/Reference/MFMessageComposeViewControllerDelegate_protocol/Reference/Reference.html#//apple_ref/occ/intfm/MFMessageComposeViewControllerDelegate/messageComposeViewController:didFinishWithResult:" target="_self">messageComposeViewController:didFinishWithResult:</a> method. The result is one of “sent,” “cancelled,” or “failed.”

Finally, the delegate is responsible for dismissing the message composition view controller, which it should do by calling the UIViewController <a href="http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/dismissModalViewControllerAnimated:" target="_self">dismissModalViewControllerAnimated:</a> method.

For a complete description of the <a href="http://developer.apple.com/library/ios/documentation/MessageUI/Reference/MFMessageComposeViewController_class/Reference/Reference.html#//apple_ref/occ/cl/MFMessageComposeViewController" target="_self">MFMessageComposeViewController</a> class, see MFMessageComposeViewController Class Reference.

Leave a Reply

电子邮件地址不会被公开。 必填项已用*标注