Receiving a call
Now that the calling interface is built, you can now add the code needed to receive a call. The VGVoiceClientDelegate has a function that is called when there is an incoming call invite. Start off by setting the client's delegate in the CallViewController's viewDidLoad function:
self.client.delegate = self
Then at the end of the CallViewController.swift file add the conformance to the VGVoiceClientDelegate.
extension CallViewController: VGVoiceClientDelegate {
func voiceClient(_ client: VGVoiceClient, didReceiveInviteForCall callId: String, from caller: String, withChannelType type: VGVoiceChannelType) {
DispatchQueue.main.async { [weak self] in
self?.displayIncomingCallAlert(callID: callId, caller: caller)
}
}
func voiceClient(_ client: VGVoiceClient, didReceiveInviteCancelForCall callId: String, with reason: VGVoiceInviteCancelReason) {
DispatchQueue.main.async { [weak self] in
self?.dismiss(animated: true)
}
}
func voiceClient(_ client: VGVoiceClient, didReceiveHangupForCall callId: String, withQuality callQuality: VGRTCQuality, reason: VGHangupReason) {
self.callID = nil
self.setHangUpButtonHidden(true)
self.setStatusLabelText("Ready to receive call...")
}
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"
}
self.setStatusLabelText(reasonString)
}
}
When there is an incoming call invite, didReceiveInviteForCall will be called. If the callee ends the call, didReceiveHangupForCall will be called. If the call invite is cancelled, didReceiveInviteCancelForCall will be called.
Next create the displayIncomingCallAlert function in the CallViewController class:
func displayIncomingCallAlert(callID: String, caller: String) {
let alert = UIAlertController(title: "Incoming call from", message: caller, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Answer", style: .default, handler: { _ in
self.client.answer(callID) { error in
if error == nil {
self.setHangUpButtonHidden(false)
self.setStatusLabelText("On a call with \(caller)")
self.callID = callID
} else {
self.setStatusLabelText(error?.localizedDescription)
}
}
}))
alert.addAction(UIAlertAction(title: "Reject", style: .destructive, handler: { _ in
self.client.reject(callID) { error in
if let error {
self.setStatusLabelText(error.localizedDescription)
}
}
}))
self.present(alert, animated: true, completion: nil)
}
The displayIncomingCallAlert function takes a call ID and a caller as a parameters. Note in the UIAlertAction for answering the invite, if successful you will assign the callID to the property from earlier and the hangup button will be visible. Add the function for ending calls to the CallViewController class:
@objc private func endCall() {
guard let callID else { return }
client.hangup(callID) { error in
if error == nil {
self.callID = nil
self.setHangUpButtonHidden(true)
self.setStatusLabelText("Ready to receive call...")
}
}
}
In the next step you will add the code needed to make a call.
Making an app to app voice call
You make a voice call from an iOS app to another iOS app