环信自定义UI
1.会话列表
添加代理
[[EMClient sharedClient] addDelegate:self delegateQueue:dispatch_get_main_queue()];
[[EMClient sharedClient].groupManager addDelegate:self delegateQueue:dispatch_get_main_queue()];
[[EMClient sharedClient].chatManager addDelegate:self delegateQueue:dispatch_get_main_queue()];
从服务远程获取会话列表
- (void)_loadAllConversationsFromDB
{
__weak typeof(self) weakSelf = self;
dispatch_async(_loadDataQueue, ^{
NSArray *conversations = [EMClient.sharedClient.chatManager getAllConversations];
if (conversations.count == 0) {
[EMClient.sharedClient.chatManager getConversationsFromServer:^(NSArray *aCoversations, EMError *aError) {
if (!aError) {
[weakSelf getAllConVFromServerOrDB:aCoversations];
}else if(aError.code == 201){
[EMClient.sharedClient loginWithUsername:weakSelf.model.easeMobUsername password:weakSelf.model.easeMobPwd completion:^(NSString *aUsername, EMError *aError) {
if (!aError) {
[weakSelf getData];
}else{
MBProgressHUDShow(@"用户信息失效,请重新登录!");
}
}];
}else{
MBProgressHUDShow(@"从服务器获取会话列表失败!");
}
}];
}else{
[weakSelf getAllConVFromServerOrDB:conversations];
}
});
}
实现代理方法
#pragma mark - EMChatManagerDelegate
//消息撤回
- (void)messagesDidRecall:(NSArray *)aMessages {
[self _loadAllConversationsFromDB];
}
//收到消息
- (void)messagesDidReceive:(NSArray *)aMessages{
if (aMessages && [aMessages count]) {
EMMessage *msg = aMessages[0];
if(msg.body.type == EMMessageBodyTypeText) {
EMConversation *conversation = [[EMClient sharedClient].chatManager getConversation:msg.conversationId type:EMConversationTypeGroupChat createIfNotExist:NO];
//群聊@“我”提醒
NSString *content = [NSString stringWithFormat:@"@%@",self.model.nickname];
if(conversation.type == EMConversationTypeGroupChat && [((EMTextMessageBody *)msg.body).text containsString:content]) {
[conversation setRemindMe:msg.messageId];
};
}
}
[self _loadAllConversationsFromDB];
}
- (void)onConversationRead:(NSString *)from to:(NSString *)to
{
[self _loadAllConversationsFromDB];
}
昵称头像获取
主要还是用户名字和头像的获取,环信提供了两个方案:1、带在消息扩展信息中。2、通过用户的环信ID从自己的服务器获取到后存储本地。
我这里是采取第二种方案,主要是由于其他端是使用的环信的UI库。
根据会话ID从服务器请求。
请求到之后存起来,刷新列表消息使用
聊天页面实现
有时间再补充
输入框以及扩展功能自定义
有时间再补充