Sending text messages from your iOS app might be something you need or want to do and we’ve seen this problem solved for our friends on Android. It’s time to solve it with Swift.

Why Shouldn’t I Send SMS from the REST API in Swift?

While Twilio is a REST API and you could make an HTTP request to it directly, you would need to store your Twilio credentials inside your app which poses a serious security issue. An attacker could decompile the application, extract your credentials and use your Twilio account for anything they liked.

I bet @SwiftOnSecurity would have a few things to say about that.

To avoid this we will create a backend application that implements the Twilio REST API, wraps up your credentials and sends SMS messages for you. Then you can call your backend application from your Swift application and send SMS messages without compromising your credentials.

Using the Twilio REST API we will also need the following tools to send a message:

Xcode 8 and Swift 3

A Twilio Account and Twilio phone number – Get yours free.

Your favorite language to create the backend. I will create a server using the twilio-python helper library. But you can choose your preference from the other server-side libraries as well.

If you’re sticking with Python, you will also need Flask, the microframework to build your backend.

A tool to handle webhooks. We’ll use ngrok. Learn more about it from Phil Nash. Alternatively, you might have a server deployed on Heroku or something similar.

Setting Up Your Python Server