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:
Then at the end of the CallViewController.swift file add the conformance to the VGVoiceClientDelegate.
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:
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