Swift

VGVoiceClient

Before you can receive a call, the Client SDK needs to authenticate to the Vonage servers. The following additions are required to ViewController.swift.

NOTE: Notice that, you have already imported VGVoiceClient at the top of the file.

Add a VonageClientSDKVoice instance, below the connectionStatusLabel.

class ViewController: UIViewController {
    ...
    let connectionStatusLabel = UILabel()
    let client = VGVoiceClient()
    ...
}

Add the JWT

At the end of viewDidLoad, set the client delegate and create a session - make sure to replace ALICE_JWT for the JWT you created during a previous step.

override func viewDidLoad() {
    ...
    VGVoiceClient.isUsingCallKit = false
    let config = VGClientConfig(region: .US)
    config.enableWebsocketInvites = true
    client.setConfig(config)
    client.delegate = self
    
    client.createSession("ALICE_JWT") { error, sessionId in
        DispatchQueue.main.async { [weak self] in
            guard let self else { return }
            if error == nil {
                self.connectionStatusLabel.text = "Connected"
            } else {
                self.connectionStatusLabel.text = error?.localizedDescription
            }
        }
    }
}

NOTE: The enableWebsocketInvites flag on the client configuration and the isUsingCallKit setting should not be used in production. Enable push notifications to make sure you get incoming calls even when your application is in the background using CallKit. See the push notifications guide for more information.

The Client Delegate

For the delegate to work, you need to have ViewController conform to VGVoiceClientDelegate. Add the extension the end of the file.

extension ViewController: VGVoiceClientDelegate {

    func client(_ client: VGBaseClient, didReceiveSessionErrorWith reason: VGSessionErrorReason) {
        let reasonString: String!
        
        switch reason {
        case .tokenExpired:
            reasonString = "Expired Token"
        case .pingTimeout, .transportClosed:
            reasonString = "Network Error"
        default:
            reasonString = "Unknown"
        }
        
        DispatchQueue.main.async { [weak self] in
            self?.connectionStatusLabel.text = reasonString
        }
    }
}

An error is shown if encountered and the connectionStatusLabel is updated with the relevant connection status.

Build and Run

Press Cmd + R to build and run again:

Interface connected