Skip to content

Receiving an incoming call

Marco Brescianini edited this page Nov 5, 2024 · 23 revisions

This guide will show you how to handle incoming calls and how to show the Kaleyra Video call UI in your app for the Kaleyra Video iOS SDK 4.0 version. If you are looking for the 3.x guide please take a look here

Table of contents

Overview

The steps required to handle an incoming call are the following:

  1. Connect the SDK
  2. Present the call user interface

Connect the SDK

Assuming the SDK is properly configured, you are required to connect it in order to start an outgoing call.

import UIKit
import KaleyraVideoSDK

@UIApplicationMain
final class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        var config = Config(appID: "My app id", region: .europe, environment: .sandbox)
        config.callKit = .enabled(.init(icon: UIImage(named: "callkit-icon")))
        config.voip = .automatic(listenForNotificationsInForeground: false)

        do {
            try KaleyraVideo.instance.configure(config)
            try KaleyraVideo.instance.connect(userId: "USER ALIAS", provider: RestAccessTokenProvider())
        } catch {
            print("Could not configure the SDK because of \(error)")
        }
        
        return true
    }
}

Present the call user interface

The last step needed in order to show an incoming call to the user is to present the call user interface. Let's pretend you have a view controller from which you want to present the call user interface; once the callPublisher emits a call object you can present the CallViewController associated with it:

import UIKit
import KaleyraVideoSDK

class MyViewController: UIViewController {

    lazy var callWindow: CallWindow = .init(windowScene: view.window!.windowScene!)

    override func viewDidLoad() {
        super.viewDidLoad()

        KaleyraVideo.instance.conference?.callPublisher.compactMap({ $0 }).receive(on: DispatchQueue.main).sink { [weak self] call in
            self?.presentCall(call)
        }.store(in: &subscriptions)
    }
    
    private func present(call: Call) {
        let controller = CallViewController(call: call, configuration: .init())
        controller.delegate = self
        callWindow.makeKeyAndVisible()
        callWindow.set(rootViewController: controller, animated: true)
    }
}

extension MyViewController: CallViewControllerDelegate {

    func callViewControllerDidFinish(_ controller: CallViewController) {
        callWindow.set(rootViewController: nil, animated: true) { _ in
            self.callWindow.isHidden = true
        }
    }
}

That's it! You don't have to do anything fancy, just present the call UI and you're good to go.

CallKit

As a side note, if you have enabled CallKit support in the SDK, when an incoming call is received the native system UI will be always displayed even if your application is in foreground and active.

Where to go from here

If you followed our guides regarding the call client you should have now a complete integration with our call system. If you haven't already, we suggest you to take a look at our sample apps (objective-c swift) to see how to handle incoming calls in a real world app. You should be now ready to move to more advanced topics, like enabling CallKit support in your app, or start integrating the VoIP notifications.

What's next

Clone this wiki locally