IOS Client SDK

Enable Telnyx real-time communication services on IOS.
The Telnyx iOS WebRTC Client SDK provides all the functionality you need to start making voice calls from an iPhone.

Project structure:

  • SDK project: sdk module, containing all Telnyx SDK components as well as tests.
  • Demo application: app module, containing a sample demo application utilizing the sdk module.

Project Setup:

  1. Clone the repository
  2. Open the cloned repository in Xcode and hit the build button to build both the sdk and sample app:
  3. Connect a device or start an emulated device and hit the run button

Usage

Adding Telnyx to your iOS Client Application:

Include the following line into your podfile and run pod install.

Copy
Copied
pod TelnyxRTC //Requires confirmation after the first release.

Note: After pasting the above content, Kindly check and remove any new line added


After updating your podfile you will need to run the following command:

Copy
Copied
pod install

Note: After pasting the above content, Kindly check and remove any new line added


Once the SDK is added to your pods, open your project workspace and import the SDK at the top of the class:

Copy
Copied
import	TelnyxRTC

Note: After pasting the above content, Kindly check and remove any new line added


Telnyx Client

The TelnyxClient connects your application to the Telnyx backend, enabling you to make outgoing calls and handle incoming calls.
The main steps to get the SDK working are: 1 - Initialize the client. 2 - Set the delegate to listen to SDK events. 3 - Connect the client: Login to the backend in order to receive and start calls. 4 - Place outbound calls. 5 - Incoming calls.


1 - Initialize the client:

To begin you will need to instantiate the Telnyx Client:


Copy
Copied
// Initialize the client
let telnyxClient = TxClient()

Note: After pasting the above content, Kindly check and remove any new line added


2 - Listen SDK updates:

To begin you will need to instantiate the Telnyx Client:


Then you will need to set the client delegate in order to get SDK events and you should implement the protocol wherever you want to receive the events:

Example of the delegate usage on a ViewController:

Copy
Copied
// Set the delegate
telnyxClient.delegate = self

Note: After pasting the above content, Kindly check and remove any new line added

Copy
Copied
extension ViewController: TxClientDelegate {

    func onRemoteCallEnded(callId: UUID) {
        // Call has been removed internally.
    }

    func onSocketConnected() {
       // When the client has successfully connected to the Telnyx Backend.
    }

    func onSocketDisconnected() {
       // When the client from the Telnyx backend
    }

    func onClientError(error: Error)  {
        // Something went wrong.
    }

    func onClientReady()  {
       // You can start receiving incoming calls or
       // start making calls once the client is fully initialized.
    }

    func onSessionUpdated(sessionId: String)  {
       // This function will be executed when a sessionId is received.
    }

    func onIncomingCall(call: Call)  {
       // Someone is calling you.
    }

    // You can update your UI from here based on the call states.
    // Check that the callId is the same as your current call.
    func onCallStateUpdated(callState: CallState, callId: UUID) {
      // handle the new call state
      switch (callState) {
      case .CONNECTING:
          break
      case .RINGING:
          break
      case .NEW:
          break
      case .ACTIVE:
          break
      case .DONE:
          break
      case .HELD:
          break
      }
    }
}

Note: After pasting the above content, Kindly check and remove any new line added


3 - Connect the client:

In this step you need to set the parameters to configure your connection. The main parameters are the login credentials used to register into the Telnyx backend.


You can connect using your SIP credentials (sip user and password) or by an access token. You can check how to create your credentials on the following link


Login with SIP credentials:

Copy
Copied
let txConfigUserAndPassword = TxConfig(sipUser:SIP_USER,
                                       password:SIP_PASSWORD)
do {
   // Connect and login
   try telnyxClient.connect(txConfig: txConfigUserAndPassword)
} catch let error {
   print("Error \(error)")
}

Note: After pasting the above content, Kindly check and remove any new line added


Login with Access Token:

Copy
Copied
// Use a JWT Telnyx Token to authenticate
let txConfigToken = TxConfig(token: "MY_JWT_TELNYX_TOKEN")

do {
   // Connect and login
   // Use `txConfigToken` 
   try telnyxClient.connect(txConfig: txConfigToken)
} catch let error {
   print("Error \(error)")
}

Note: After pasting the above content, Kindly check and remove any new line added


The TxConfig Structure:

Through the TxConfig structure you can configure the following optional parameters:

  • The Ringtone audio file.
  • The Ringback tone audio file.
  • The logging level.

4 - Outbound calls:

Once your client is fully connected and logged in you can place outbound calls from your iPhone. You can call to a phone number or to a SIP URI. To call a SIP URI pass the following as the destinationNumber parameter: YOURDESTINATIONSIP_USER@sip.telnyx.com


Your client will be ready to place outbound calls only when is logged in. To determine whether your client is ready or not, you need to implement the TxClientDelegate and wait for the onClientReady() method to be called.


Example of usage:

Copy
Copied
 // Create a client instance
   self.telnyxClient = TxClient()

   // Assign the delegate to get SDK events
   self.telnyxClient?.delegate = self

   // Connect the client (Check TxClient class for more info)
   self.telnyxClient?.connect(....)

   // Create the call and start calling
   self.currentCall = try self.telnyxClient?.newCall(callerName: "Caller name",
                                                     callerNumber: "155531234567",
                                                     // Destination is required and can be a phone number or SIP URI
                                                     destinationNumber: "18004377950",
                                                     callId: UUID.init())

Note: After pasting the above content, Kindly check and remove any new line added


5 - Incoming calls:

In order to answer an incoming call you will need to be fully connected and logged in.


Copy
Copied
//Init your client
func initTelnyxClient() {
   self.telnyxClient = TxClient()

   // Assign the delegate to get SDK events
   self.telnyxClient?.delegate = self

   // Connect the client (Check TxClient class for more info)
   self.telnyxClient?.connect(....)
}

extension ViewController: TxClientDelegate {
    //....
    func onIncomingCall(call: Call) {
        //We are automatically answering any incoming call as an example, but
        //maybe you want to store a reference of the call, and answer the call after a button press.
        self.myCall = call.answer()
    }
}

Note: After pasting the above content, Kindly check and remove any new line added


Before uploading your app to TestFlight:

The WebRTC SDK requires access to the device microphone in order to make audio calls. In order to avoid rejection from Apple when uploading a build to TestFlight, remember to add the following key into your info.plist:

  1. Privacy - Microphone Usage Description

Microphone permission will be requested when making the first outbound call or when receiving the first inbound call.


Questions? Comments? Building something rad? Join our Slack channel and share.