Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Getting Started with the Voice iOS SDK


Want to add VoIP calling to your iOS application?

Start by downloading the Voice Quickstart for Swift(link takes you to an external page) application source code. Prefer working with Objective-C? Follow the quickstart on the Voice Quickstart for Objective-C(link takes you to an external page) source code instead.

Next, follow the step-by-step instructions below (for Swift) to get you up and running quickly using Twilio's Programmable Voice SDK.


Twilio Voice Quickstart for iOS

twilio-voice-quickstart-for-ios page anchor

Please see our iOS 13 Migration Guide(link takes you to an external page) for the latest information on iOS 13.


Get started with Voice on iOS

get-started-with-voice-on-ios page anchor
  • Quickstart - Run the swift quickstart app
  • Examples - Sample applications



To get started with the quickstart application follow these steps. Steps 1-5 will enable the application to make a call. The remaining steps 6-9 will enable the application to receive incoming calls in the form of push notifications using Apple's VoIP Service.

1. Install the TwilioVoice framework

1-install-the-twiliovoice-framework page anchor

Swift Package Manager

Twilio Voice is now distributed via Swift Package Manager. To consume Twilio Voice using Swift Package Manager, add the https://github.com/twilio/twilio-voice-ios repository as a Swift Pacakge.

2. Use Twilio CLI to deploy access token and TwiML application to Twilio Serverless

2-use-twilio-cli-to-deploy-access-token-and-twiml-application-to-twilio-serverless page anchor

You must have the following installed:

Run npm install to install all dependencies from NPM.

Install twilio-cli with:


_10
npm install -g twilio-cli

Login to the Twilio CLI. You will be prompted for your Account SID and Auth Token, both of which you can find on the dashboard of your Twilio console(link takes you to an external page).


_10
twilio login

Once successfully logged in, an API Key, a secret get created and stored in your keychain as the twilio-cli password in SKxxxx\|secret format. Please make a note of these values to use them in the Server/.env file.

This app requires the Serverless plug-in(link takes you to an external page). Install the CLI plugin with:


_10
twilio plugins:install @twilio-labs/plugin-serverless

Before deploying, create a Server/.env by copying from Server/.env.example


_10
cp Server/.env.example Server/.env

Update Server/.env with your Account SID, auth token, API Key and secret


_10
ACCOUNT_SID=ACxxxx
_10
AUTH_TOKEN=xxxxxx
_10
API_KEY_SID=SKxxxx
_10
API_SECRET=xxxxxx
_10
APP_SID=APxxxx (available in step 3)
_10
PUSH_CREDENTIAL_SID=CRxxxx (available in step 6)

The Server folder contains a basic server component which can be used to vend access tokens or generate TwiML response for making call to a number or another client. The app is deployed to Twilio Serverless with the serverless plug-in:


_10
cd Server
_10
twilio serverless:deploy

The server component that's baked into this quickstart is in Node.js. If you'd like to roll your own or better understand the Twilio Voice server side implementations, please see the list of starter projects in the following supported languages below:

Follow the instructions in the project's README to get the application server up and running locally and accessible via the public Internet.

3. Create a TwiML application for the Access Token

3-create-a-twiml-application-for-the-access-token page anchor

Next, we need to create a TwiML application. A TwiML application identifies a public URL for retrieving TwiML call control instructions. When your iOS app makes a call to the Twilio cloud, Twilio will make a webhook request to this URL, your application server will respond with generated TwiML, and Twilio will execute the instructions you've provided.

Use Twilio CLI to create a TwiML app with the make-call endpoint you have just deployed (Note: replace the value of --voice-url parameter with your make-call endpoint you just deployed to Twilio Serverless)


_10
$ twilio api:core:applications:create \
_10
--friendly-name=my-twiml-app \
_10
--voice-method=POST \
_10
--voice-url="https://my-quickstart-dev.twil.io/make-call"

You should receive an Appliciation SID that looks like this


_10
APxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

4. Generate an access token for the quickstart

4-generate-an-access-token-for-the-quickstart page anchor

Install the token plug-in


_10
twilio plugins:install @twilio-labs/plugin-token

Use the TwiML App SID you just created to generate an access token


_10
twilio token:voice --identity=alice --voice-app-sid=APxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Copy the access token string. Your iOS app will use this token to connect to Twilio.

5. Run the Swift Quickstart app

5-run-the-swift-quickstart-app page anchor

Now let's go back to the VoiceQuickstart.xcworkspace. Update the placeholder of accessToken with access token string you just copied


_14
import UIKit
_14
import AVFoundation
_14
import PushKit
_14
import CallKit
_14
import TwilioVoice
_14
_14
let accessToken = "PASTE_YOUR_ACCESS_TOKEN_HERE"
_14
let twimlParamTo = "to"
_14
_14
let kCachedDeviceToken = "CachedDeviceToken"
_14
_14
class ViewController: UIViewController {
_14
...
_14
}

Build and run the app. Leave the text field empty and press the call button to start a call. You will hear the congratulatory message. Support for dialing another client or number is described in steps 8 and 9. Tap "Hang Up" to disconnect.

``

6. Create a Push Credential with your VoIP Service Certificate

6-create-a-push-credential-with-your-voip-service-certificate page anchor

The Programmable Voice SDK uses Apple's VoIP Services to let your application know when it is receiving an incoming call. If you want your users to receive incoming calls, you'll need to enable VoIP Services in your application and generate a VoIP Services Certificate.

Go to Apple Developer portal(link takes you to an external page) and generate a VoIP Service Certificate.

Once you have generated the VoIP Services Certificate, you will need to provide the certificate and key to Twilio so that Twilio can send push notifications to your app on your behalf.

Export your VoIP Service Certificate as a .p12 file from Keychain Access and extract the certificate and private key from the .p12 file using the openssl command.


_10
openssl pkcs12 -in PATH_TO_YOUR_P12 -nokeys -out cert.pem -nodes
_10
openssl x509 -in cert.pem -out cert.pem
_10
openssl pkcs12 -in PATH_TO_YOUR_P12 -nocerts -out key.pem -nodes
_10
openssl rsa -in key.pem -out key.pem

Use Twilio CLI to create a Push Credential using the cert and key.


_10
$ twilio api:chat:v2:credentials:create \
_10
--type=apn \
_10
--sandbox \
_10
--friendly-name="voice-push-credential (sandbox)" \
_10
--certificate="$(cat PATH_TO_CERT_PEM)" \
_10
--private-key="$(cat PATH_TO_KEY_PEM)"

This will return a Push Credential SID that looks like this


_10
CRxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

The --sandbox option tells Twilio to send the notification requests to the sandbox endpoint of Apple's APNS service. Once the app is ready for distribution or store submission, create a separate Push Credential with a new VoIP Service certificate without the --sandbox option.

Note: we strongly recommend using different Twilio accounts (or subaccounts) to separate VoIP push notification requests for development and production apps.

Now let's generate another access token and add the Push Credential to the Voice Grant.


_10
$ twilio token:voice \
_10
--identity=alice \
_10
--voice-app-sid=APxxxx \
_10
--push-credential-sid=CRxxxxs

7. Receive an incoming call

7-receive-an-incoming-call page anchor

You are now ready to receive incoming calls. Update your app with the access token generated from step 6 and rebuild your app. The TwilioVoiceSDK.register() method will register your mobile client with the PushKit device token as well as the access token. Once registered, hit your application server's /place-call endpoint: https://my-quickstart-dev.twil.io/place-call?to=alice. This will trigger a Twilio REST API request that will make an inbound call to the identity registered on your mobile app. Once your app accepts the call, you should hear a congratulatory message.

Register your mobile client with the PushKit device token:


_10
TwilioVoiceSDK.register(accessToken: accessToken, deviceToken: cachedDeviceToken) { error in
_10
if let error = error {
_10
NSLog("An error occurred while registering: \(error.localizedDescription)")
_10
} else {
_10
NSLog("Successfully registered for VoIP push notifications.")
_10
}
_10
}

Please note that your application must have voip enabled in the UIBackgroundModes of your app's plist in order to be able to receive push notifications.

``

8. Make client to client call

8-make-client-to-client-call page anchor

To make client to client calls, you need the application running on two devices. To run the application on an additional device, make sure you use a different identity in your access token when registering the new device.

Use the text field to specify the identity of the call receiver, then tap the "Call" button to make a call. The TwiML parameters used in TwilioVoice.connect() method should match the name used in the server.

``

9. Make client to PSTN call

9-make-client-to-pstn-call page anchor

To make client to number calls, first get a verified Twilio number to your account via https://www.twilio.com/console/phone-numbers/verified(link takes you to an external page). Update your server code and replace the callerNumber variable with the verified number. Restart the server so it uses the new value.

``


You will also find additional examples that provide more advanced use cases of the Voice SDK:


You can find the API documentation of the Voice SDK:


To learn more about how to use TwiML and the Programmable Voice Calls API, check out our TwiML quickstarts:


Please file any issues you find here on Github: Voice Swift Quickstart(link takes you to an external page). Please ensure that you are not sharing any Personally Identifiable Information(PII) or sensitive account information (API keys, credentials, etc.) when reporting an issue.

For general inquiries related to the Voice SDK you can file a support ticket(link takes you to an external page).


MIT


Rate this page: