Sending Text Message through SMS in Swift
Posted By : Akshay Luthra | 17-Dec-2015
NOTE : Simulator is unable to send text messages, therefore you’ll have to run the code written in this blog on an actual device to see its working.
First, create a class, say MessageComposer, that will handle the responsibility of creating/configuring a text message composer.
import Foundation import MessageUI let textMessageRecipients = [] // for pre-populating the recipients list class MessageComposer: NSObject, MFMessageComposeViewControllerDelegate { // A wrapper function to indicate whether or not a text message can be sent from the user's device func canSendText() -> Bool { return MFMessageComposeViewController.canSendText() } // Configures and returns a MFMessageComposeViewController instance func configuredMessageComposeViewController() -> MFMessageComposeViewController { let messageComposeVC = MFMessageComposeViewController() messageComposeVC.messageComposeDelegate = self // Make sure to set this property to self, so that the controller can be dismissed! messageComposeVC.recipients = textMessageRecipients messageComposeVC.body = “Sending Text Message through SMS in Swift" return messageComposeVC } // MFMessageComposeViewControllerDelegate callback - dismisses the view controller when the user is finished with it func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) { controller.dismissViewControllerAnimated(true, completion: nil) } }
Finally, make a View Controller to create an instance of this MessageComposerclass and present it based upon some user action, such as tapping a button.
import UIKit class ViewController: UIViewController { // Create a MessageComposer let messageComposer = MessageComposer() @IBAction func sendTextMessageButtonTapped(sender: UIButton) { // Make sure the device can send text messages if (messageComposer.canSendText()) { // Obtain a configured MFMessageComposeViewController let messageComposeVC = messageComposer.configuredMessageComposeViewController() // Present the configured MFMessageComposeViewController instance presentViewController(messageComposeVC, animated: true, completion: nil) } else { // Let the user know if his/her device isn't able to send text messages let errorAlert = UIAlertView(title: "Cannot Send Text Message", message: "Your device is not able to send text messages.", delegate: self, cancelButtonTitle: "OK") errorAlert.show() } } }
That's all you need to do. Thanks for reading this blog!
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Akshay Luthra
Akshay Luthra has excellent experience in developing Cross Platform Mobile Apps using JavaScript and Titanium Framework. He loves listening to music and travelling new places.