Looking to add Voice, Video, Chat, or Sync to your iOS mobile application? You'll need to provide an access token from your server to the Twilio library for that product.
There are two steps to getting this set up - the first is to create an access token for your end user's mobile application from your server. For instance, based on their user profile, they could have the ability to join a video room for a tutoring session with another user. You can learn more about how to create access tokens from the programming language of your choice (Java, C#, Ruby, Python, PHP, or Node.js). If you're looking to experiment, try out one of our SDK Starter projects as a server. If you don't have one in particular you want to use, the SDK Starter for Node.js is pretty straight forward.
The second step is to request that access token from your server application - which is what we will cover in this guide. We will use the standard iOS networking library to make an HTTPS call to our server.
Our code for retrieiving the access token will differ slightly, based on whether we are using Swift or Objective-C. If you're new to iOS development, Swift is probably easier to pick up and get started with.
We will use the
NSURLSession with Objective-C) class as the basis for our approach. The iOS networking library uses configurable sessions that can create individual tasks that perform a network request. These tasks can either retrieve data, download a file, or upload a file - all of which are different subclasses of the
NSURLSessionTask class. To retrieve an access token, we would use a data task, which would be an instance of the
NSURLSessionDataTask class. You create a data task from a
NSURLSession object - usually you would use the
shared singleton object available on the
Typically, we would use an HTTP GET request to retrieve an access token from a given URL.
dataTask function on
URLSession that we will use takes a URL and a closure (completion block in Objective-C) as an argument. Your closure will get the HTTP response, the
NSData returned by the server, and an error (if there was one) as arguments. We check to see if the error exists, and if it does not, we create a string from the
Data and print it out. Be sure to call resume on the task to initiate the HTTP Request - this step is easily forgotten.
When you use the Xcode playground with Swift to try out HTTP networking, the playground will send the HTTP request, and then immediately move on to the next line of code - when the playground gets to the last line of code, execution will stop, and your HTTP response won't be logged. You will need to enable a special setting for Xcode playgrounds and network calls if you are trying this outside of a mobile app.
To do this, you will need to import the
PlaygroundSupport framework, and then include this line of code at the bottom:
PlaygroundPage.current.needsIndefiniteExecution = true
When we call the
/token route from one of the SDK Starter Kit servers, the response will come back as JSON with an identity and a corresponding access token. Using the new
Decodable protocol in Swift 4, we can have a struct that models this response. See Apple's documentation for more on encoding and decoding JSON with Swift.
In our particular case, the struct will contain two strings - identity and token - and only needs to be decodable as we aren't encoding data as JSON to send it back to the server.
Be sure to wrap the JSON decoding in a do...try...catch block, so that you can catch any JSON parsing errors.
Whether you are using your own server to provide access tokens or one of our SDK starter kit servers, you can see how easy it is to retrieve an access token using iOS' standard library.