TxClient

public class TxClient

The TelnyxRTC client connects your application to the Telnyx backend, enabling you to make outgoing calls and handle incoming calls.

Examples

Connect and login:

// Initialize the client
let telnyxClient = TxClient()

// Register to get SDK events
telnyxClient.delegate = self

// Setup yor connection parameters.

// Set the login credentials and the ringtone/ringback configurations if required.
// Ringtone / ringback tone files are not mandatory.
// You can user your sipUser and password
let txConfigUserAndPassowrd = TxConfig(sipUser: sipUser,
                                       password: password,
                                       ringtone: "incoming_call.mp3",
                                       ringBackTone: "ringback_tone.mp3",
                                       //You can choose the appropriate verbosity level of the SDK.
                                       //Logs are disabled by default
                                       logLevel: .all)

// Use a JWT Telnyx Token to authenticate (recommended)
let txConfigToken = TxConfig(token: "MY_JWT_TELNYX_TOKEN",
                             ringtone: "incoming_call.mp3",
                             ringBackTone: "ringback_tone.mp3",
                             //You can choose the appropriate verbosity level of the SDK. Logs are disabled by default
                             logLevel: .all)

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

// You can call client.disconnect() when you're done.
Note: you need to relese the delegate manually when you are done.

// Disconnecting and Removing listeners.
telnyxClient.disconnect();

// Release the delegate
telnyxClient.delegate = nil

Listen TxClient delegate events.

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 was 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 base on the call states.
    // Check that the callId is the same as your current call.
    func onCallStateUpdated(callState: CallState, callId: UUID) {
        DispatchQueue.main.async {
            switch (callState) {
            case .CONNECTING:
                break
            case .RINGING:
                break
            case .NEW:
                break
            case .ACTIVE:
                break
            case .DONE:
                break
            case .HELD:
                break
            }
        }
    }
}

Properties

  • Keeps track of all the created calls by theirs UUIDs

    Declaration

    Swift

    public internal(set) var calls: [UUID : Call] { get }
  • Subscribe to TxClient delegate to receive Telnyx SDK events

    Declaration

    Swift

    public weak var delegate: TxClientDelegate?
  • When implementing CallKit framework, audio has to be manually handled. Set this property to TRUE when provider(CXProvider, didActivate: AVAudioSession) is called on your CallKit implementation Set this property to FALSE when provider(CXProvider, didDeactivate: AVAudioSession) is called on your CallKit implementation

    Declaration

    Swift

    public var isAudioDeviceEnabled: Bool { get set }
  • Undocumented

    Declaration

    Swift

    public func enableAudioSession(audioSession: AVAudioSession)
  • Undocumented

    Declaration

    Swift

    public func disableAudioSession(audioSession: AVAudioSession)
  • Client must be registered in order to receive or place calls.

    Declaration

    Swift

    public var isRegistered: Bool { get }

Initializers

  • TxClient has to be instantiated.

    Declaration

    Swift

    public init()

Connection handling

  • Connects to the iOS cloglient to the Telnyx signaling server using the desired login credentials.

    Throws

    TxConfig parameters errors

    Declaration

    Swift

    public func connect(txConfig: TxConfig, serverConfiguration: TxServerConfiguration = TxServerConfiguration()) throws

    Parameters

    txConfig

    The desired login credentials. See TxConfig docummentation for more information.

    serverConfiguration

    (Optional) To define a custom signaling server and TURN/ STUN servers. As default we use the internal Telnyx Production servers.

  • Disconnects the TxClient from the Telnyx signaling server.

    Declaration

    Swift

    public func disconnect()
  • To check if TxClient is connected to Telnyx server.

    Declaration

    Swift

    public func isConnected() -> Bool

    Return Value

    true if TxClient socket is connected, false otherwise.

  • To answer and control callKit active flow

    Declaration

    Swift

    public func answerFromCallkit(answerAction: CXAnswerCallAction, customHeaders: [String : String] = [:])

    Parameters

    answerAction

    CXAnswerCallAction from callKit

    customHeaders

    (Optional)

  • To end and control callKit active and conn

    Declaration

    Swift

    public func endCallFromCallkit(endAction: CXEndCallAction, callId: UUID? = nil)
  • To disable push notifications for the current user

    Declaration

    Swift

    public func disablePushNotifications()
  • Get the current session ID after logging into Telnyx Backend.

    Declaration

    Swift

    public func getSessionId() -> String

    Return Value

    The current sessionId. If this value is empty, that means that the client is not connected to Telnyx server.

Call handling

  • This function can be used to access any active call tracked by the SDK. A call will be accessible until has ended (transitioned to the DONE state).

    Declaration

    Swift

    public func getCall(callId: UUID) -> Call?

    Parameters

    callId

    The unique identifier of a call.

    Return Value

    TheCall object that matches the requested callId. Returns nil if no call was found.

  • Creates a new Call and starts the call sequence, negotiate the ICE Candidates and sends the invite.

    Throws

    • sessionId is required if user is not logged in
    • socket connection error if socket is not connected
    • destination number is required to start a call.

    Declaration

    Swift

    public func newCall(callerName: String,
                        callerNumber: String,
                        destinationNumber: String,
                        callId: UUID,
                        clientState: String? = nil,
                        customHeaders:[String:String] = [:]) throws -> Call

    Parameters

    callerName

    The caller name. This will be displayed as the caller name in the remote’s client.

    callerNumber

    The caller Number. The phone number of the current user.

    destinationNumber

    The destination SIP user address (sip:YourSipUser@sip.telnyx.com) or phone number.

    callId

    The current call UUID.

    clientState

    (optional) Custom state in string format encoded in base64

    customHeaders

    (optional) Custom Headers to be passed over webRTC Messages, should be in the format X-key:Value X is required for headers to be passed.

    Return Value

    The call that has been created

Push Notifications handling

  • Call this function to process a VoIP push notification of an incoming call. This function will be executed when the app was closed and the user executes an action over the VoIP push notification. You will need to

    Throws

    Error during the connection process

    Declaration

    Swift

    public func processVoIPNotification(txConfig: TxConfig,
                                        serverConfiguration: TxServerConfiguration,pushMetaData:[String: Any]) throws

    Parameters

    txConfig

    The desired configuration to login to B2B2UA. User credentials must be the same as the

    serverConfiguration

    required to setup from VoIP push notification metadata.

    pushMetaData

    meta data payload from VOIP Push notification (this should be gotten from payload.dictionaryPayload[“metadata”] as? [String: Any])

Audio

  • Select the internal earpiece as the audio output

    Declaration

    Swift

    public func setEarpiece()
  • Select the speaker as the audio output

    Declaration

    Swift

    public func setSpeaker()