Swift

Recevoir un appel

Maintenant que l'interface d'appel est construite, vous pouvez ajouter le code nécessaire pour recevoir un appel. Le code VGVoiceClientDelegate possède une fonction qui est appelée lorsqu'il y a une invitation à un appel entrant. Commencez par définir le délégué du client dans le fichier CallViewController's viewDidLoad fonction :

self.client.delegate = self

Puis, à la fin de la CallViewController.swift ajoute la conformité au fichier 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)
    }
}

Lorsqu'il y a une invitation à un appel entrant, didReceiveInviteForCall sera appelé. Si l'appelant met fin à l'appel, didReceiveHangupForCall sera appelé. Si l'invitation à l'appel est annulée, didReceiveInviteCancelForCall sera appelé.

Créez ensuite le fichier displayIncomingCallAlert dans la fonction CallViewController classe :

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)
}

Les displayIncomingCallAlert prend comme paramètres l'identifiant de l'appel et l'appelant. Notez que dans la fonction UIAlertAction pour avoir répondu à l'invitation. Si l'invitation est acceptée, vous attribuerez à la personne qui a répondu à l'invitation la mention callID à la propriété précédente et le bouton de raccrochage sera visible. Ajoutez la fonction de fin d'appel à la propriété CallViewController classe :

@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...")
        }
    }
}  

Dans l'étape suivante, vous ajouterez le code nécessaire pour passer un appel.