Skip to content

Opening Chat

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

This guide will show you how to interact with the ChatViewController. If you are integrating a 3.x version of the SDK you should take a look at this guide instead.

Table of contents:

Overview

The ChatViewController is the UI component responsible for presenting the user interface of a chat. These are the steps you need to take in order to present it:

  1. Connect the SDK
  2. Create an instance.
  3. Present the user interface

From now on we are assuming you read the previous guides regarding the conference object.

Connect the SDK

Assuming the SDK is properly configured, you are required to connect it in order to present the chat user interface.

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

Creating a ChatViewController instance

In order to tell the SDK who you want to chat with, you must create a ChatViewController instance. You must provide this object the user id for the user you want to chat with, or the id of a chat. You should also provide a configuration object if you want to customize which buttons should be presented in its navigation bar.

let controller = ChatViewController(intent: .chat(participant: "alice"), configuration: .init(audioButton: true, videoButton: true))

Presenting a ChatViewController

Once you have created a ChatViewController instance you can present it modally and respond to its events subscribing as its delegate.

final class MyViewController: UIViewController {

    func openChat(with user: String) {
        let controller = ChatViewController(intent: .chat(participant: user), configuration: .init(audioButton: true, videoButton: true))
        controller.delegate = self
        self.controller.present(controller, animated: true)
    }
}

extension MyViewController: ChatViewControllerDelegate {

    func chatViewControllerDidFinish(_ controller: ChatViewController) {
        controller.dismiss(animated: true)
    }

    func chatViewControllerDidTapAudioCallButton(_ controller: ChatViewController) {
        print("Start a voice call with \(controller.participants)")
    }

    func chatViewControllerDidTapVideoCallButton(_ controller: ChatViewController) {
        print("Start a video call with \(controller.participants)")
    }
}

That's it! the steps you've seen are the only steps required to start sending/receiving messages.

Where to go from here

The next guide In-app notifications will teach you how to show an in-app notification every time a message is received. Also, if you haven't already, we suggest you to take a look at our sample app to see how to start sending/receiving messages in a real app.

What's next

Clone this wiki locally