获取会话
SDK 在连接状态下会自动同步云端的会话列表并保存到本地数据库,你可以从本地数据库获取 SDK 保存的完整会话列表并根据相关会话属性自行决定展示方式。
获取会话列表
通过以下接口获取 SDK 在本地数据库保存的会话列表。获取到的会话列表按照最新消息时间倒序排列,置顶会话会排在最前。
接口定义
swift
func getChatList() -> [KIMChat]
Objective-C
- (NSArray<KIMChat *> *)getChatList;
代码示例
swift
let list = KIMCore.shared.getChatList()
监听会话列表变化
当接收到新消息、会话属性变更、会话已读未读等将导致会话列表数据或排序变更,将触发会话列表刷新。你可以通过设置代理 KIMChatListDelegate
的方式监听会话列表刷新变更。
接口定义
swift
func register(chatListDelegate: KIMChatListDelegate)
Objective-C
- (void)registerWithChatListDelegate:(id <KIMChatListDelegate> _Nonnull)chatListDelegate;
参数说明
属性 | 返回值 | 说明 |
---|---|---|
chatListDelegate | IKIMChatListDelegate | 监听代理 |
KIMChatListDelegate
说明:
方法名称 | 参数 | 返回值 | 说明 |
---|---|---|---|
chatListDelegate | KIMChatListDelegate | Void | 会话列表更新 |
didUpdateChatListStatus | status: KIMChatListStatusstatus: KIMChatListStatus 列表加载状态, 0 非加载状态 1 加载中 2 加载完成 | Void | 会话列表加载状态更新, status:0 非加载状态 1 加载中 2 加载完成 |
代码示例
swift
KIMCore.shared.register(chatListDelegate: self)
获取会话列表中指定会话
获取本地数据
接口定义
swift
func getChat(chatId: String) -> KIMChat?
Objective-C
- (KIMChat * _Nullable)getChatWithChatId:(NSString *)chatId
参数说明
属性 | 类型 | 说明 |
---|---|---|
chatId | String | 会话 ID |
代码示例
swift
let chat = KIMCore.shared.getChat(chatId: chatId)
获取云端数据
接口定义
swift
func getChat(chatId: String, complete: ((_ chat: KIMChat?, _ error: KIMError?) -> Void)?)
Objective-C
- (void)getChatWithChatId:(NSString *)chatId complete:(void (^ _Nullable)(KIMChat * _Nullable, KIMError * _Nullable))complete;
参数说明
属性 | 类型 | 说明 |
---|---|---|
chatId | String | 会话 ID |
complete | (KIMChat, KIMError) -> Void | 完成回调。 KIMChat:会话信息. KIMError:错误信息。 |
代码示例
swift
KIMCore.shared.getChat(chatId: chatId) { chat, error in
}
获取指定会话详情
接口定义
swift
func getChatInfo(chatId: String, complete: ((_ chatInfo: KIMChatInfo?, _ error: KIMError?) -> Void)?)
Objective-C
- (void)getChatInfoWithChatId:(NSString *)chatId complete:(void (^ _Nullable)(KIMChatInfo * _Nullable, KIMError * _Nullable))complete;
参数说明
参数 | 类型 | 说明 |
---|---|---|
chatId | String | 会话 ID |
complete | (KIMChatInfo, KIMError) -> Void | 完成回调。 KIMChatInfo:会话详情信息. KIMError:错误信息。 |
代码示例
swift
KIMCore.shared.getChatInfo(chatId: chatId) { chatInfo, error in
}