While developing an iOS application you might have got a scenario where you require to send a text message and you would be baffling around with Why? How? And What?


In this tutorial we will be focussing on how to send text message from your iOS application in Swift, where we will be sending a text message from your user’s phone number. While we cannot do this directly without the content of your user’s but we can display a precomposed message for the user to send which user can modify later if he wants to.

So let’s get started,

We will be using “MFMessageComposeViewController” class object to display the standard message composition interface inside your application.

Before we present the composition interface, we will populate the fields with the basic initial message which we wish to send, list of recipients which a user can modify later.

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.

Step 1: Open Xcode → New Projecr → Single View Application → Let’s name it “MailComposer”

Step 2: Open Main.storyboard and add a button and name it “Send Mail”, do not forget to add constraints “Horizontally and Vertically in the container” and add “width = 100 points and height to 50 points” as show below.

Step 3: Navigate to ViewController.swift and add the button instance IBAction and name it “sendMessage”

@IBAction func sendMessage(_ sender: Any) {
}

Step 4: Add “MFMessageComposeViewControllerDelegate” protocol to your ViewController class, and add stub method to ensure it conforms to that protocol. Import “MessageUI”in ViewController.swift

Step 5: Create new method “displayMsgInterface” below the viewDidLoad and paste the below code, which will be responsible to present the MFMessageComposeViewController and configuration of prefilled recipients and message.

func displayMsgInterface() {
   let messageCompose = MFMessageComposeViewController()
   messageCompose.messageComposeDelegate = self
   // Configure recipients
   messageCompose.recipients = ["9401234567"]
   // Configure message body
   messageCompose.body = "Hey! I just learned how to send message using iOS App."
   // Present the ViewController modally
   if MFMessageComposeViewController.canSendText() {
      self.present(messageCompose, animated: true, completion: nil)
   } else {
      print("You cannot send messages.")
   }
}

Step 6: Add the following code to the delegate method “didFinishWithResult”

func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result:
MessageComposeResult) {
   self.dismiss(animated: true, completion: nil)
}

Step 7: Call “displayMsgInterface” method from “viewDidLoad”.

Your final code should look like below!

And we’re done! Let’s run it.

To see it running you need to run in real device as text messaging behaviour is not available in simulator.

Updated on: 30-Jul-2019

62 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements