In-app Chat UIKit
  • iOS : Swift
  • Android
  • Web
  • Overview
  • Quick start
    • Integrate the SDK
    • Start a chat
  • Run the sample code
  • UI components
  • SDK downloads
  • Release notes

Start a chat

Last updated:2022-11-14 14:55

In-app Chat UIKit supports the following:

  1. Start a one-on-one chat by entering a user ID.
  2. Create a group chat by entering a set of user IDs.
  3. Join a group chat by entering a group ID.

In this guide, we will show you how to quickly start a one-on-one chat/group chat, and how to join a group chat respectively.

Prerequisites

Integrate the In-app Chat UIKit SDK into your project (finished the initialization and login are required). For more information, see Integrate the SDK.

Start a one-on-one chat

Whether starting a one-on-one chat or a group chat, the peer user you want to chat with/the users you want to invite to a chat group must have logged in to the In-app Chat UIKit at least once. Otherwise, an error will occur.

  1. Get the userID that is generated using your own business logic. (the userID here refers to the peer user you want to chat with.)
  2. Fill in the userID parameter and run the following code:
import UIKit
import ZIMKit

/// your ViewController.swift
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    // Start a 1-on-1 chat after logging in successfully.
    func startOneOnOneChat(userID: String) {
        let messageVC = MessagesListVC(conversationID: userID, type: .peer)
        messageVC.modalPresentationStyle = .fullScreen
        present(messageVC, animated: true)
        // Replace the [present] method with the following code if you already use the Navigation Controller.
        // navigationController?.pushViewController(messageVC, animated: true)
    }
}

Start a group chat

  1. Get the userIDs and groupName that is generated using your own business logic. (the userIDs here refers to the ID list of the users that you want to invite to the group chat.)
  2. Fill in the userIDs and groupName parameters and run the following code:
import UIKit
import ZIMKit

/// your ViewController.swift
class ViewController1: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    func createGroupChat(userIDs: [String], groupName: String) {
        ZIMKitManager.shared.createGroup(groupName: groupName, userIDs: userIDs) { [weak self] groupInfo, userErrors, error in
            if error.code == .success {
                if userErrors.count > 0 {
                    // Implement the logic for the prompt window based on your business logic when there is a non-existing user ID in the group.

                } else {
                    // Directly enter the chat page when the group chat is created successfully.
                    self?.showGroupMessageListVC(groupID: groupInfo.id)
                }
            } else {
                // Implement the logic for the prompt window based on the returned error info when failing to create a group chat. 

            }
        }
    }

    func showGroupMessageListVC(groupID: String) {
        let messageVC = MessagesListVC(conversationID: groupID, type: .group)
        messageVC.modalPresentationStyle = .fullScreen
        present(messageVC, animated: true)
        // Replace the [present] method with the following code if you already use the Navigation Controller.
        // navigationController?.pushViewController(messageVC, animated: true)
    }
}

Join a group chat

  1. Get the groupID that is generated using your own business logic. (the groupID here refers to the group chat you want to join.)
  2. Fill in the groupID parameter and run the following code:
import UIKit
import ZIMKit

/// your ViewController.swift
class ViewController1: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    func joinGroupChat(_ groupID: String) {
        ZIMKitManager.shared.joinGroup(by: groupID) { [weak self] groupInfo, error in
            if error.code == .success {
                // Display the group chat page after joining a group chat successfully.
                self?.showGroupMessageListVC(groupID: groupInfo.id)
            }
        }
    }

    func showGroupMessageListVC(groupID: String) {
        let messageVC = MessagesListVC(conversationID: groupID, type: .group)
        messageVC.modalPresentationStyle = .fullScreen
        present(messageVC, animated: true)
        // Replace the [present] method with the following code if you already use the Navigation Controller.
        // navigationController?.pushViewController(messageVC, animated: true)
    }
}

More to explore

Get support

Need help or want to raise your questions? Click the button below to join our Discord community to get quick responses.

Join Discord community