Skip to content

Getting Started

Marco Brescianini edited this page Nov 8, 2024 · 19 revisions

This guide will show you how to start integrating the Kaleyra Video iOS SDK 4.x versions. If you are looking for the 3.x versions guide take a look at this guide instead

Getting started

This guide will show you the basic steps you need to make in order to integrate the Kaleyra Video in your iOS app. From now on we are assuming you already have obtained your Bandyer credentials.

Table of contents

SDK configuration

The first thing we need to do in order to use the Kaleyra Video iOS SDK is configuring and initializing it. In the code samples below, we are going to initialize the Kaleyra Video iOS SDK in our sample AppDelegate. We do it there for convenience, but you are not required to do so, you can initialize the Kaleyra Video iOS SDK anywhere you want in your code, just make sure to do it only once. You must create a configuration value the SDK will read to configure itself during initialization. You are required to provide three parameters to the value:

  • The appID identifying your company in the Kaleyra Video platform (beware this identifier is bound to a particular environment and region, if you choose the wrong combination you might not be able to connect your client)
  • The region you want your client to connect to.
  • The environment you want your client to connect to

Let's pretend we want to configure the SDK for the sandbox environment, in europe region, enabling CallKit, automatic VoIP notifications handling:

In Swift you'd do like this:

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)
        } catch {
            print("Could not configure the SDK because of \(error)")
        }
        
        return true
    }
}

As you might have noticed, once we set all the flags and configuration needed on the configuration object, we told the SDK to configure itself providing it the configuration object created previously. This step, sets up the SDK internally, but you won't be able to make outgoing calls yet. You must connect the SDK before you can do anything.

Connecting the SDK

The next step you must perform in order for the SDK to start processing incoming calls and accept outgoing call is connect it providing an access token provider and the id of the user you want to connect. Once your app user has been authenticated you should connect the SDK calling the connect method on the KaleyraVideo singleton instance

Let's pretend we are writing an app that has some kind of authentication mechanism. After we have authenticated our user (either she/he has provided hers/his authentication credential on a login screen or we resumed her/his user session) we show her/him our wonderful app main screen. There we are going to connect the Kaleyra Video SDK.

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

When the user signs off don't forget to call the disconnect method on the KaleyraVideo singleton instance. That method will stop any running client and it will close the user session.

Access token provider

Starting from 3.0 version the Kaleyra Video SDK uses a strong authentication mechanism based on JWT access tokens. Anytime the SDK needs to authenticate a user or refresh the user session it will call the provide access token method on the AccessTokenProvider instance provided to SDK in the connect method asking a for an access token.

In the code snippets below you'll find an example of an AccessTokenProvider retreving the access token from a "fake" back-end through a REST call:

import Foundation
import KaleyraVideoSDK

final class RestAccessTokenProvider: AccessTokenProvider {

    private let client: HTTPClient = .shared
    
    private lazy var decoder: JSONDecoder = .init()

    func provideAccessToken(userId: String, completion: @escaping (Result<String, Error>) -> Void) {
        let request = URLRequest(url: URL(string: "https://my-app.com/user/access_token?user_id=\(userId)")!)

        client.post(request) { (result) in
            completion(Result {
                switch result {
                    case let .success(result):
                        do {
                            if (200..<300).contains(result.response.statusCode) {
                                return try decoder.decode(String.self, from: result.data)
                            } else {
                                throw BadRequestError()
                            }
                        } catch let(error) {
                            throw error
                        }
                    case let .failure(error):
                        throw error
                }
            })
        }
    }
}

Where to go from here

Now that you have a basic implementation that can connect to the Kaleyra Video platform you are ready to start making outgoing calls or receive incoming calls. We also recommend you to take a look at the call client lifecycle guide that will explain you how to respond to the call client state change events.

What's next

Clone this wiki locally