环信SDK集成
请参考iOS开发-在现有项目中集成环信SDK及EaseUI或者参考官方文档
此操作只针对已集成环信聊天
聊天头像的设置
聊天头像设置就是在聊天对象中携带用户信息的相关参数,我们使用EMMessage
中的扩展字段ext
携带用户信息
我们创建一个EMUserInfo
的用户信息类
- EMUserInfo.h
@interface EMUserInfo : NSObject
/*!
用户环信ID(环信账号)
*/
+ (NSString *)current_id_user;
/*!
用户头像的URL
*/
+ (NSString *)current_heading_user;
/*!
用户昵称
*/
+ (NSString *)current_name_user;
/**
当前用户信息
@return 用户信息字典
*/
+ (NSDictionary *)currentUserInfo;
@end
- EMUserInfo.m
/*!
用户ID
*/
+ (NSString *)current_id_user {
return @"获取本地保存的环信账号";
}
/*!
用户头像的URL
*/
+ (NSString *)current_heading_user {
return @"获取本地保存的用户头像";
}
/*!
用户昵称
*/
+ (NSString *)current_name_user {
return @"获取本地保存的用户昵称";
}
// 在APP登陆或者获取用户信息的时候,需要保存相关信息
+ (NSDictionary *)currentUserInfo {
return @{@"from_id_user":@"获取本地保存的环信账号",
@"from_name_user":@"获取本地保存的用户昵称",
@"from_heading_user":@"获取本地保存的用户头像"
};
}
- 在发送消息的时候携带用户信息
EaseMessageViewController.m
中- (void)sendMessage:(EMMessage *)message isNeedUploadFile:(BOOL)isUploadFile
添加相关代码
message.ext = [EMUserInfo currentUserInfo];
- 聊天页面
新建一个控制器BSChatViewController
继承EaseMessageViewController
相关代码
- (void)viewDidLoad {
[super viewDidLoad];
// 设置EaseMessageViewControllerDataSource的代理
self.dataSource = self;
}
#pragma mark - EaseMessageViewControllerDataSource
- (id<IMessageModel>)messageViewController:(EaseMessageViewController *)viewController modelForMessage:(EMMessage *)message {
id<IMessageModel> model = nil;
model = [[EaseMessageModel alloc] initWithMessage:message];
if (model.isSender) {//自己发送
//头像
model.avatarURLPath = [EMUserInfo current_heading_user];
//昵称
model.nickname = [EMUserInfo current_name_user];
//头像占位图
model.avatarImage = kPlaceholderImage;
}else{//对方发送
model.avatarImage = kPlaceholderImage;
//头像
model.avatarURLPath = message.ext[@"from_heading_user"];
//昵称
model.nickname = @"";
}
return model;
}
会话列表页面头像设置
需要做本地存储:存储的时机就是进入聊天页面的时候把对方的相关信息保存下来
在EMUserInfo中添加如下代码
EMUserInfo.h
/**
本地保存对方聊天信息
@param userId 环信账号
@param userName 用户名
@param avatarURLPath 头像
*/
+ (void)saveToUserInfo:(NSString *)userId name:(NSString *)userName avatarURLPath:(NSString *)avatarURLPath;
/**
查找本地用户信息
@param userId 环信账号
@return 信息字典
*/
+ (NSDictionary *)findUserInfoByUserId:(NSString *)userId;
EMUserInfo.m
+ (void)saveToUserInfo:(NSString *)userId name:(NSString *)userName avatarURLPath:(NSString *)avatarURLPath {
NSString *filePatch = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"messageList.plist"];
NSMutableArray *array =[[NSMutableArray alloc] initWithContentsOfFile:filePatch];
if (!array) {
array = [NSMutableArray array];
}
NSDictionary *toDict = @{@"from_id_user":userId,
@"from_name_user":userName,
@"from_heading_user":avatarURLPath
};
if (![array containsObject:toDict]) {
[array addObject:toDict];
[array writeToFile:filePatch atomically:true];
}
}
+ (NSDictionary *)findUserInfoByUserId:(NSString *)userId {
NSString *filePatch = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"messageList.plist"];
NSMutableArray *array =[[NSMutableArray alloc] initWithContentsOfFile:filePatch];
NSDictionary *tempDict;
for (NSDictionary *dict in array) {
if ([dict[@"from_id_user"] isEqualToString:userId]) {
tempDict = dict;
break;
}
}
return tempDict;
}
新建BSMessageListViewController
继承EaseConversationListViewController
相关代码
- (void)viewDidLoad {
[super viewDidLoad];
// 设置EaseConversationListViewControllerDataSource的代理
self.dataSource = self;
}
- (id<IConversationModel>)conversationListViewController:(EaseConversationListViewController *)conversationListViewController
modelForConversation:(EMConversation *)conversation {
EaseConversationModel *model = [[EaseConversationModel alloc] initWithConversation:conversation];
if (model.conversation.type == EMConversationTypeChat) {
NSDictionary *dict = conversation.lastReceivedMessage.ext;
if(dict[@"from_name_user"] == nil || dict[@"from_heading_user"] == nil){
NSDictionary *localDict = [EMUserInfo findUserInfoByUserId:conversation.conversationId];
model.title = [localDict objectForKey:@"from_name_user"];
model.avatarURLPath = [localDict objectForKey:@"from_heading_user"];
model.avatarImage = kPlaceholderImage;
}else{
model.title = dict[@"from_name_user"];
model.avatarURLPath = dict[@"from_heading_user"];
//头像占位图
model.avatarImage = kPlaceholderImage;
}
}
return model;
}
这样就实现了会话列表头像和聊天界面头像的展示