iOS学习笔记

环信聊天对话页面双方头像以及名称

2017-03-29  本文已影响112人  勇不言弃92

环信聊天页面对方以及自己的头像和昵称/名称,两种方式1.在自己的聊天页面“ChatViewController.m”中(自己命名,可能会有文件名差异)修改2.修改了“EaseMessageViewController.m”文件

1.ChatViewController.m

在自己写的文件中实现下面这个方法,此方法简单但是有时聊天页面会不显示头像与名称

- (id<IMessageModel>)messageViewController:(EaseMessageViewController *)viewController
modelForMessage:(EMMessage *)message
{
id<IMessageModel> model = nil;
model.avatarImage = [UIImage imageNamed:@"EaseUIResource.bundle/user"];
model = [[EaseMessageModel alloc] initWithMessage:message];
model.failImageName = @"EaseUIResource.bundle/imageDownloadFail";

model.avatarURLPath = [message.ext objectForKey:@"IMicon"];
model.nickname = [message.ext objectForKey:@"IMname"];

if (message.direction==EMMessageDirectionSend) {
//发送的信息,显示自己姓名与头像
model.avatarURLPath = @"https://raw.githubusercontent.com/ly92/images/master/icons/icon1.jpeg";
model.nickname = @"小勇";

model.message.ext = @{@"IMname" : @"小勇",
                      @"IMicon" : @"https://raw.githubusercontent.com/ly92/images/master/icons/icon1.jpeg"
                     };

}
}
else{
//如果是聊天超级管理员,则不处理
if ([self.conversation.conversationId isEqualToString:@"admin"]) {
model.nickname = @"管理员";
return model;
}
//其他会员
else{
//对方环信帐号信息获取
model.nickname = @"舒洁";
model.avatarURLPath = @"https://raw.githubusercontent.com/ly92/images/master/icons/icon2.jpeg";

model.message.ext = @{@"IMname" : @"舒洁",
@"IMicon" : @"https://raw.githubusercontent.com/ly92/images/master/icons/icon2.jpeg"
};
}
return model;
}

2.EaseMessageViewController.m

这个方法也没有什么难的,但是需要改引入的三方库,后面升级三方库会比较麻烦,但是每次头像和名称都会显示出来
1.在“- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath”方法中加上两行,在每次为cell赋值model之前加上

model.avatarURLPath = [model.message.ext objectForKey:@"IMicon"];
model.nickname = [model.message.ext objectForKey:@"IMname"];

2.在“- (void)_sendMessage:(EMMessage *)message”方法中发送数据前加上头像和名称的信息

- (void)_sendMessage:(EMMessage *)message
{
if (self.conversation.type == EMConversationTypeGroupChat){
message.chatType = EMChatTypeGroupChat;
}
else if (self.conversation.type == EMConversationTypeChatRoom){
message.chatType = EMChatTypeChatRoom;
}

//下面几行为发送的信息添加头像和名称的信息
NSMutableDictionary *ext = [NSMutableDictionary dictionaryWithDictionary:message.ext];
UserModel *user = [[LocalData sharedInstance] getSaleAccount];
if (user){
[ext setObject:user.name forKey:@"IMname"];
[ext setObject:user.image forKey:@"IMicon"];
}
message.ext = ext;


[self addMessageToDataSource:message
progress:nil];

__weak typeof(self) weakself = self;
[[EMClient sharedClient].chatManager asyncSendMessage:message progress:nil completion:^(EMMessage *aMessage, EMError *aError) {
[weakself.tableView reloadData];
}];
}

3.在“- (void)didReceiveMessages:(NSArray *)aMessages”方法接收数据后加上头像和名称的信息,iOS与安卓如果不能同时去做环信聊天功能就不可避免的去自己缓存聊天对象的名称与头像,下面几行是在每次接收到信息时根据聊天对象的环信号来获取头像与名称信息,然后加入到收到的信息中-----2016-11-21

- (void)didReceiveMessages:(NSArray *)aMessages
{
for (EMMessage *message in aMessages) {
if ([self.conversation.conversationId isEqualToString:message.conversationId]) {

NSMutableDictionary *ext = [NSMutableDictionary dictionaryWithDictionary:message.ext];
ChatUser *chatuser = [[ChatUserManager shareInstance] fetchChatUserByIm:message.conversationId];
if (chatuser){
[ext setObject:chatuser.name forKey:@"IMname"];
[ext setObject:chatuser.avatar forKey:@"IMicon"];
}
message.ext = ext;

[self addMessageToDataSource:message progress:nil];

[self _sendHasReadResponseForMessages:@[message]
isRead:NO];

if ([self _shouldMarkMessageAsRead])
{
[self.conversation markMessageAsReadWithId:message.messageId];
}
}
}
}

特别注意

使用第二种方案时需要改引入的三方库,所以迁移代码时需要做相应的调整

上一篇下一篇

猜你喜欢

热点阅读