logo
当前页

快速开始

这份文档将指导您集成 IMKit 并快速开始聊天。

前提条件

  • 前往ZEGO管理控制台并执行以下操作:
    1. 创建一个项目,并获取您的项目的AppIDAppSign
    2. 激活即时通讯服务。
  • 准备环境:
    • Xcode 13.0或更高版本
    • 运行在iOS 12.0或更高版本的iOS设备
    • 设备已连接到互联网。

集成 SDK

说明

CocoaPods 版本要求是 1.10.0 或更高。

1

使用CocoaPods将 ZIMKit 添加为您项目的依赖项

打开 Podfile 文件并添加 pod 'ZIMKit'

Podfile
target 'MyProject' do
  use_frameworks!
# !mark
  pod 'ZIMKit'
end

执行命令 pod repo update 来更新本地索引,确保您可以安装最新版本的 SDK。

pod repo update

执行 pod install 命令来安装 SDK。

pod install
2

设置相关权限

打开 Info.plist 文件并添加以下内容:

<key>NSMicrophoneUsageDescription</key>
<string>We require microphone access to use zimkit.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>We require photo access to use zimkit.</string>
3

调用 init 方法来初始化 IMKit

AppDelegate.swift
import UIKit
import ZIMKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        let appID: UInt32 = <#your appID#> // The AppID you get from ZEGOCLOUD Admin Console.
        let appSign: String = <#your appSign#> // The AppSign you get from ZEGOCLOUD Admin Console.
// !mark
        ZIMKit.initWith(appID: appID, appSign: appSign)
    }
}
4

在登录页面上调用 connectUser 方法以登录到 IMKit

警告

您可以自定义规则来生成用户ID和用户名。我们建议您设置一个有意义的用户ID。您可以将用户ID与您的业务账户系统关联起来。

viewController.swift
import UIKit
import ZIMKit

/// your viewController.swift
class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let connectUserButton = UIButton(type: .custom)
        connectUserButton.setTitle("Connect User And Show", for: .normal)
        connectUserButton.frame = .init(x: 100, y: 100, width: 250, height: 50)
        connectUserButton.addTarget(self, action: #selector(connectUserAction), for: .touchUpInside)
        connectUserButton.backgroundColor = .orange
        view.addSubview(connectUserButton)

    }

    @objc func connectUserAction(_ sender: Any) {
        // Your ID as a user.
        let userID: String = "<#your userID#>" 
        // Your name as a user.
        let userName: String = "<#your userName#>" 
        // The image you set as the user avatar must be network image. e.g., https://doc-media.zego.im/IMKit/avatar/avatar-0.png
        let userAvatarUrl: String = "<#your userAvatarUrl#>" 

// !mark(1:6)
        ZIMKit.connectUser(userID: userID, userName: userName, avatarUrl: userAvatarUrl) { error in
            //  Display the UI views after connecting the user successfully. 
            if error.code == .success {
                self?.showConversationListVC()
            }  
        }
    }
}
5

显示 IMKit 的会话组件

viewController.swift
/// your viewController.swift
func showConversationListVC() {
    let conversationVC = ZIMKitConversationListVC()
    let nav = UINavigationController(rootViewController: conversationVC)
    nav.modalPresentationStyle = .fullScreen
    self.present(nav, animated: true)
}

理想情况下,到这个时候,你的应用程序应该是这个样子的:

开始聊天

IMKit 支持以下功能:

警告

无论是开始一对一聊天还是群聊,你想要聊天的用户或者你想要邀请的群聊成员必须至少登录过 IMKit 一次。否则,将会出现错误。

  1. 使用您自己的业务逻辑生成userId(这里的userId是指您想要与之聊天的对等用户)。
  2. 填写userId参数并运行以下代码:
ViewController.swift
import UIKit
import ZIMKit

/// your ViewController.swift
class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    // 在成功登录后,您可以在任何需要的地方调用此方法。
    func startOneOnOneChat(userID: String) {
        let messageVC = ZIMKitMessagesListVC(conversationID: userID, type: .peer)
        messageVC.modalPresentationStyle = .fullScreen
        present(messageVC, animated: true)

        // 如果您已经使用 Navigation Controller,请使用以下代码替换[present]方法。
        // navigationController?.pushViewController(messageVC, animated: true)
    }
}
  1. 使用您自己的业务逻辑获取生成的 idsgroupName。(这里的 ids 指的是您想邀请加入群聊的用户的ID列表。)
  2. 填写 idsgroupName 参数,并运行以下代码:
ViewController.swift
import UIKit
import ZIMKit

/// your ViewController.swift
class ViewController1: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    // 在成功登录后,您可以在任何需要的地方调用此方法。
    func createGroupChat(userIDs: [String], groupName: String) {
        ZIMKit.createGroup(with: groupName, inviteUserIDs: userIDs) { [weak self] groupInfo, inviteUserErrors, error in
            if error.code == .success {
                if inviteUserErrors.count > 0 {
                    // 当群组中存在一个不存在的用户ID时,根据您的业务逻辑实现提示窗口的逻辑。

                } else {
                    // 成功创建群聊后直接进入聊天页面。
                    self?.showGroupMessageListVC(groupID: groupInfo.id)
                }
            } else {
                // 根据创建群聊失败时返回的错误信息,实现提示窗口的逻辑。

            }
        }
    }
    
    func showGroupMessageListVC(groupID: String) {
        let messageVC = ZIMKitMessagesListVC(conversationID: groupID, type: .group)
        messageVC.modalPresentationStyle = .fullScreen
        present(messageVC, animated: true)

        // 如果您已经使用 Navigation Controller,请使用以下代码替换[present]方法。
        // navigationController?.pushViewController(messageVC, animated: true)
    }
}
  1. 使用您自己的业务逻辑生成groupId(这里的groupId指的是您想要加入的群聊)。
  2. 填写groupId参数并运行以下代码:
ViewController.swift
import UIKit
import ZIMKit

/// your ViewController.swift
class ViewController1: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }

    // 在成功登录后,您可以在任何需要的地方调用此方法。
    func joinGroupChat(_ groupID: String) {
        ZIMKit.joinGroup(by: groupID) { [weak self] groupInfo, error in
            if error.code == .success {
                // 成功加入群聊后显示群聊页面。
                self?.showGroupMessageListVC(groupID: groupInfo.id)
            }
        }
    }
    
    func showGroupMessageListVC(groupID: String) {
        let messageVC = ZIMKitMessagesListVC(conversationID: groupID, type: .group)
        messageVC.modalPresentationStyle = .fullScreen
        present(messageVC, animated: true)

        // 如果您已经使用 Navigation Controller,请使用以下代码替换[present]方法。
        // navigationController?.pushViewController(messageVC, animated: true)
    }
}

相关指南

Previous

概述

Next

概述

当前页

返回到顶部