How to Send a Custom iMessage in Swift

This tutorial will show how you can send custom texts from your app in Swift. Sending texts can be useful in different ways! You can send a text to invite another User to download your app or even send texts to share achievements from an app!

This tutorial will go over:

  1. Creating a Messages View Controller
  2. Setting the View Controller’s Delegate
  3. Creating and Handling a message 

Creating a Messages View Controller

In order to trigger the send text event, drag a button into a View Controller that the User can click to send a message.

Screen Shot 2016-12-31 at 2.16.09 PM.png

Then create an IBAction for the Button in your ViewController.swift file.

Screen Shot 2016-12-31 at 2.18.59 PM.png

Setting the View Controller’s Delegate

Next, import the MessageUI library and make the View Controller a MFMessageComposeViewControllerDelegate. This will require that you implement the method messageComposeViewController(controller:, didFinishWith:) method. This method will be called once the User finished sending a text message.

Screen Shot 2016-12-31 at 2.20.25 PM.png

More information about View Controller delegates

Creating and Handling a Message

The following code creates a message. The commented numbers in the code are explained in the numbered list below.

Screen Shot 2016-12-31 at 2.37.51 PM.png

  1. First check to see if the phone is capable of sending a text message.
  2. Create a new MFMessageComposeViewController to present to the User. This will be where the User will draft their message.
  3. Set the controller’s default text message body, recipients, and set the delegate. If the recipients are left empty the User can just pick who they would like to send it to. Setting the delegate will ensure that the delegate method we added earlier will be called when the User sends a message.
  4. Present the View Controller.

Lastly add the following line of code to the delegate method.

controller.dismiss(animated: true, completion: nil)

This will ensure that the controller is dismissed when the text is sent, so the User can return back to the main app.

The resulting View Controller should look something like the following.

Screen Shot 2016-12-31 at 2.59.37 PM.png

That’s it! Now try running your app and you should be able to send a message! You can also try letting the User choose who the recipient of the message should be. This can be done by adding a Text Field to allow the User to enter a phone number.

Note*

To test this app, you will need to run it on an actual device. The emulator cannot send texts and therefore the canSendText() method will return false and nothing will happen when you click the button.

Extendability

Now that you know how to send texts within an app, you can use this to accomplish a few different tasks! Sending texts can serve as a way to invite other’s to download your app on the App Store or to share achievements within an app!


Article Written By,

Thomas Oropeza

thomasoropeza@gmail.com / www.thomasoropeza.com