Security is at the top of everyone’s mind and phone verification is a simple way to secure your application and help prevent bot accounts. Phone verification is a common security tool used when people sign up for a product or give you their phone number for the first time.

Confidence in your users’ phone numbers decreases fraud and increases reliability of notifications. Let’s take a look at how to verify phone numbers from an iOS application using Swift and the Twilio Verify API.

This tutorial will walk you through the process of SMS phone verification step by step. You can check out the final project on my GitHub.

What you’ll need

To code along with this post, you’ll need:

Setting up

Navigate to the Twilio Console and grab your Verify Service SID.

We don't want to store our environment variables directly in our iOS application, so we'll need a backend server that will talk to the Verify API. Head over to the sample backend on my GitHub and click the "Deploy to Heroku" button. This is a small application written in Python, your backend can be in whatever language you'd like (here are other examples using Twilio Functions, Node.js, and Golang). The important thing is that it manages the sensitive environment variables instead of the iOS application. You'll be redirected to Heroku; give your app a name and "Deploy app". Next, hit "Manage App" and navigate to Settings.

In Settings, add your TWILIO_ACCOUNT_SID , TWILIO_AUTH_TOKEN , and VERIFY_SERVICE_SID as Heroku config variables. Copy your Heroku Domain URL, we'll need this soon.

Open up Xcode and create a new Project. Choose “Single View App”, give your product a name like Phone Verification , and hit Create .



Next, create a new file (shortcut: ⌘N), choose the Property List template, and name the file Config.plist. This will store your Heroku URL.

Add your Heroku Domain URL under the Root with the key of serverUrl and value of your URL.



If you’re tracking your project using Git, make sure to add Config.plist to your .gitignore file. This is all the setup we should need to get our app started.

Phone Verification made easy with Twilio Verify

In order to do phone verification, our app will follow this workflow:

The user enters two pieces of information to our application: Phone number Country code

Our application makes a request to the Verify API to initiate the verification

The user receives a text message with a 6 digit code

The user then enters that code into a form in the application

The application sends the code off to the Verify API, with the user’s phone number and country code, to check the verification

If it is correct then the user has verified their number

With only two API calls and three views, you’ll be able to verify your users’ phone numbers.

Phone Verification in Swift

Let’s test out our setup in Swift. We’ll be using URLSession to make our HTTP requests, but my coworker Sam has a full overview of the other ways to make HTTP requests in Swift 3.