Global IM UIKit 快速开始
本页用 5 步完成 Global IM UIKit 接入:导入 ImpIaaSIMUIKit → 初始化 IMLib 客户端 → 创建 ViewModel → 组装 ViewController → 运行并验证(会话列表 / 聊天 / 发送 / 已读 / 主题)。Swift 与 Objective-C 示例并列给出。
- 环境与导入
iOS 13+、Xcode 15+;CocoaPods 添加 `ImpIaaSIMUIKit`(自动依赖 IMLib / IMKit),或 SPM 添加包。
- 初始化 IMLib
`ImpIaaSIM.client(options:)` 创建客户端并 `connect`;UIKit 不重复建连。
- 创建 ViewModel
用客户端创建 `ChatListViewModel` / `ChatViewModel`,注入 DataSource / options。
- 组装 ViewController
把 ViewModel、Theme、Delegate 传给 `ChatListViewController` / `ChatViewController`。
- 运行与验证
进入会话列表 → 打开聊天 → 发送文本 → 已读/撤回 → 切换主题;宿主实现 Delegate 的导航与详情。
1. 环境与导入
# Podfile
platform :ios, '13.0'
target 'YourApp' do
pod 'ImpIaaSIMUIKit', '~> 1.0' # 自动依赖 ImpIaaSIM / ImpIaaSIMKit
end// Package.swift 或 Xcode → Add Package Dependencies
.package(url: "https://example.com/ImpIaaSIMUIKit.git", from: "1.0.0")2. 初始化 IMLib
import ImpIaaSIM
import ImpIaaSIMUIKit
let client = ImpIaaSIM.client(options: IMPClientOptions(
appkey: "YOUR_APP_KEY",
serverUrl: "https://im.example.com"
))
let res = await client.connect(token: TOKEN)
if res.code != 0 { print("connect failed:", res.code, res.msg ?? "") }#import <ImpIaaSIM/ImpIaaSIM.h>
#import <ImpIaaSIMUIKit/ImpIaaSIMUIKit.h>
IMPOSClient *client = [[IMPOSClient alloc] initWithAppkey:@"YOUR_APP_KEY"
serverUrl:@"https://im.example.com"];
[client connectWithToken:TOKEN completion:^(IMPOSResult *result) {
if (result.code != 0) { NSLog(@"connect failed: %ld %@", (long)result.code, result.msg); }
}];3. 创建 ViewModel
// 会话列表状态机
let listViewModel = IMPChatListViewModel(
client: client,
dataSource: nil,
options: nil
)
// 聊天页状态机(单聊示例)
let chatViewModel = IMPChatViewModel(
client: client,
conversationType: .privateChat,
targetId: "user2",
options: nil
)IMPOSGlobalChatListViewModel *chatListViewModel =
[[IMPOSGlobalChatListViewModel alloc] initWithClient:client dataSource:nil options:nil];
IMPOSGlobalChatViewModel *chatViewModel =
[[IMPOSGlobalChatViewModel alloc] initWithClient:client conversationType:1 /* 单聊 */ targetId:@"user2" options:nil];ViewModel 的事件:stateChanged(state)、error(err) / messagesChanged(change)、sendStateChanged(localId,state)、typingChanged(userId,typing)。
4. 组装 ViewController
该示例引用了 SDK 头文件未导出的符号(多为 Swift 专用 helper)。请使用下表真实 ObjC 类,或参考对应组件页的 Objective-C 接入说明。
| 可用 ObjC 类 | 用途 |
|---|---|
IMPOSClient | IMLib 客户端(连接、消息、会话、群组等) |
IMPOSChatViewController | IMKit 会话页(真实 ObjC 类) |
IMPOSConversationListViewController | IMKit 会话列表页(真实 ObjC 类) |
IMPOSGlobalChatViewController | Global UIKit 会话页(真实 ObjC 类) |
IMPOSGlobalChatListViewController | Global UIKit 会话列表页(真实 ObjC 类) |
// 组装 Global UIKit 会话列表页并交给宿主导航
let listViewController = IMPChatListViewController(
viewModel: listViewModel,
theme: .light,
delegate: nil,
options: nil
)
navigationController.pushViewController(listViewController, animated: true)5. 运行与验证
- 会话列表:进入页面后 ViewModel 触发加载,列表展示会话、未读与连接态横幅。
- 聊天:点击会话由宿主 push `ChatViewController`;发送文本后气泡经历发送中 → 已发送 → 已读。
- 已读/撤回:进入会话自动已读(可关闭),撤回消息替换为占位。
- 主题:运行中切换 `ChatUITheme`,页面即时生效,无需重建。
- 两个账号验证收发:同进程只允许一个登录态,用两台模拟器或真机 + 模拟器。
`ChatListDelegate` / `ChatDelegate` 只回调事件;push / present 由宿主或 `ChatRouter` 完成,避免 UIKit 与宿主路由耦合。
组件索引
| 组件 | 职责 | 文档 |
|---|---|---|
ChatListViewController | 会话列表页(列表/搜索/连接态/空态/骨架) | 查看 |
ChatViewController | 聊天页(消息流/输入/历史/已读/撤回) | 查看 |
ChatListViewModel | 会话列表状态机 | 查看 |
ChatViewModel | 聊天页状态机 | 查看 |
ChatListDataSource | 会话列表数据源协议 | 查看 |
ChatListDelegate | 会话列表交互回调协议 | 查看 |
ChatDataSource | 消息列表数据源协议 | 查看 |
ChatDelegate | 聊天页交互回调协议 | 查看 |
ChatCellModel | 消息渲染模型 | 查看 |
ChatInputController | 输入区控制器 | 查看 |
ChatRouter | 统一路由(宿主可替换) | 查看 |
ChatUITheme | Global UIKit 主题令牌 | 查看 |
下一步:架构分层、与 IMKit 的关系与迁移、主题定制。