环信SDK 头像、昵称、表情自定义和群聊设置的实现 二(附源码)
前言:
环信SDK 头像、昵称、表情自定义和群聊设置的实现 一(附源码)
接着上面说的,我们来说说表情,它在哪里可以自定义,怎么写,那个方法是添加表情的我们都说说,找到 ChatViewController.m文件,它里面有这个方法;
-(NSArray*)emotionFormessageViewController:(EaseMessageViewController *)viewController
你可以在这里方法里面添加自己的表情,先看看Demo里面的源码,上面有自己的一些注释;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | -( NSArray *)emotionFormessageViewController:(EaseMessageViewController *)viewController{ NSMutableArray *emotions = [ NSMutableArray array]; for ( NSString *name in [EaseEmoji allEmoji]) { // 默认表情 EaseEmotion *emotion = [[EaseEmotion alloc] initWithName:@ "" emotionId:name emotionThumbnail:name emotionOriginal:name emotionOriginalURL:@ "" emotionType:EMEmotionDefault]; [emotions addObject:emotion]; } EaseEmotion *temp = [emotions objectAtIndex:0]; EaseEmotionManager *managerDefault = [[EaseEmotionManager alloc] initWithType:EMEmotionDefault emotionRow:3 emotionCol:7 emotions:emotions tagImage:[UIImage imageNamed:temp.emotionId]]; // 下面是自己添加的Png格式的表情 // NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"normal_face" ofType:@"plist"]]; // NSMutableArray * nameData = [[NSMutableArray alloc] initWithCapacity:array.count]; // NSMutableArray * emotionPngs = [NSMutableArray array]; // _emotionDic = [NSMutableDictionary dictionary]; // for (NSDictionary * dic in array) { // // NSString * nameString = [dic objectForKey:@"face_name"]; // [nameData addObject:nameString]; // } // int index = 0; // for (NSString * faceName in nameData) { // // index++; // EaseEmotion *emotion = [[EaseEmotion alloc] initWithName:faceName emotionId:[NSString stringWithFormat:@"em%d",(2000+index)] emotionThumbnail:faceName emotionOriginal:faceName emotionOriginalURL:@"" emotionType:EMEmotionPng]; // [emotionPngs addObject:emotion]; // [_emotionDic setObject:emotion forKey:[NSString stringWithFormat:@"em%d",(2000+index)]]; // } // // EaseEmotionManager *managerPng= [[EaseEmotionManager alloc] initWithType:EMEmotionPng emotionRow:3 emotionCol:7 emotions:emotionPngs tagImage:[UIImage imageNamed:@"[调皮]"]]; // 这是Demo里面兔斯基的表情,你要重新添加自己的Gif格式表情的话可以在这里按照这个方法进行添加 NSMutableArray *emotionGifs = [ NSMutableArray array]; NSArray *names = @[@ "icon_002" ,@ "icon_007" ,@ "icon_010" ,@ "icon_012" ,@ "icon_013" ,@ "icon_018" ,@ "icon_019" ,@ "icon_020" ,@ "icon_021" ,@ "icon_022" ,@ "icon_024" ,@ "icon_027" ,@ "icon_029" ,@ "icon_030" ,@ "icon_035" ,@ "icon_040" ]; int Gifindex = 0; for ( NSString * name in names) { Gifindex++; EaseEmotion *emotion = [[EaseEmotion alloc] initWithName:[ NSString stringWithFormat:@ "动图%d" ,Gifindex] emotionId:[ NSString stringWithFormat:@ "em%d" ,(2000 + Gifindex)] emotionThumbnail:[ NSString stringWithFormat:@ "%@_cover" ,name] emotionOriginal:[ NSString stringWithFormat:@ "%@" ,name] emotionOriginalURL:@ "" emotionType:EMEmotionGif]; [emotionGifs addObject:emotion]; [_emotionDic setObject:emotion forKey:[ NSString stringWithFormat:@ "em%d" ,(2000 + Gifindex)]]; } //EaseEmotionManager *managerGif= [[EaseEmotionManager alloc] initWithType:EMEmotionGif emotionRow:2 emotionCol:4 emotions:emotionGifs tagImage:[UIImage imageNamed:@"icon_002_cover"]]; return @[managerDefault]; } |
可能有些小伙伴会和我一样,很纠结这个Demo默认的这套表情到底在哪里?看看下面的图你就能找到了。
恩就是这个EaseConvertToCommonEmoticonsHelper.m文件,上面截图的代码里面的表情名称是为了和Android统一改的,这里也不用纠结!
再说说你获取到的群聊的名称和群图片,看看下面这张APP的截图;
其实在这里你可以换一种思路去做这件事,不一定要经过后台,你可以叫后台的创建群的时候,或者是你自己在前端创建群的时候,群主题你可以写成群的名称,群的描述米可以把群群图片的URL写进去,换了个方式获取到它们了,这样获取到的群也就没啥问题了。这里的代码就不粘贴了,因为这部分只是这样说说,不是大家关心的地方了。要有疑问可以在我主页找到我QQ,QQ问我!
群的设置
这里的群的设置也是Demo里面有的功能,我们主要就说说这里的内容,先看看下面的APP截图;
这个群里面有多少人,得和后台配合一起来做,让后台写接口你请求群里面所有人!重点说说这几个,屏蔽群消息,接受并提示群消息,查找聊天记录和清空聊天纪录;
一:屏蔽群消息
在SDK的文件EMClient.h中有这样一个属性
1 2 3 4 5 | /*! * \~chinese * 群组模块 */ @property ( nonatomic , strong, readonly ) id <IEMGroupManager> groupManager; |
这个属性可以用来管理屏蔽等这些属于群设置的内容,你通过 [EMClient sharedClient].groupManager 就可以取到它了,然后剩下的就是按部就班的操作,给大家随便写一个,比如下面这是一个完整的屏蔽群消息和取消的方法,说下面三点:
一:群主是不能屏蔽群消息的。
二:屏蔽了群消息你记得还有关闭接受并提醒这个操作,因为你屏蔽之后也就相应的收不到消息了。
三:刚进入这个设置界面,你得判断你是不是屏蔽了这个群的消息,用的就是 isBlocked 的属性,下面的接收提醒的是一个道理,刚进来都要判断你是不是开了!当然这些都是SDK里面的属性,你知道会用就行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | -( void )chatSetSwitchIsChanged:(UISwitch *)swit{ // NOTE:这里加判断,要是是群主就不能屏蔽消息 // NSSLog(@"===%@",READUSERDEFAULTS(UserPhonenumber)); // NSSLog(@"===%@",_chatGroup.owner); if ([[ NSString stringWithFormat:@ "%@" ,READUSERDEFAULTS(UserPhonenumber)] isEqualToString:_chatGroup.owner]) { [MBPSecondary showMBProgressHUDWithText:@ "群主不能屏蔽消息" andType:FAILED]; self .chatSetSwitch.on = NO ; return ; } // 打开屏蔽群消息 if ([swit isOn]){ [MBPSecondary showMBProgressHUDWithText:@ "屏蔽中..." ToView: self .view]; [[EMClient sharedClient].groupManager blockGroup:_chatGroup.groupId completion:^(EMGroup *aGroup, EMError *aError) { [MBPSecondary hideMBProgressHUDWith: self .view]; if (aError) { [MBPSecondary showMBProgressHUDWithText:@ "屏蔽失败" andType:FAILED]; self .chatSetSwitch.on = NO ; } else { // 屏蔽成功的话要关闭远程推送 [[EMClient sharedClient].groupManager updatePushServiceForGroup: self .chatGroup.groupId isPushEnabled: NO completion:^(EMGroup *aGroup, EMError *aError){ if (aError) { [MBPSecondary showMBProgressHUDWithText:@ "屏蔽失败" andType:SUCCESSFUL]; self .chatSetSwitch.on = NO ; } else { [MBPSecondary showMBProgressHUDWithText:@ "屏蔽成功" andType:SUCCESSFUL]; self .chatAndPromptSetSwitch.on = NO ; } }]; self .chatSetSwitch.on = YES ; } }]; } else { // 取消屏蔽群消息 [MBPSecondary showMBProgressHUDWithText:@ "取消中..." ToView: self .view]; [[EMClient sharedClient].groupManager unblockGroup:_chatGroup.groupId completion:^(EMGroup *aGroup, EMError *aError) { [MBPSecondary hideMBProgressHUDWith: self .view]; if (aError) { [MBPSecondary showMBProgressHUDWithText:@ "取消失败" andType:FAILED]; self .chatSetSwitch.on = YES ; } else { [MBPSecondary showMBProgressHUDWithText:@ "取消成功" andType:FAILED]; self .chatAndPromptSetSwitch.on = YES ; self .chatSetSwitch.on = NO ; } }]; } } |
查找聊天记录:
这个功能当然也是SDK里面有的,这里有几个地方你要注意的,我也在下面代码的注释里面有加,下面这个方法是当上面的搜索框一档输入的内容有变化就执行一次:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #pragma mark -- SearchBarSearch //每次有输入内容更新就会调用这个方法 - ( void )updateSearchResultsForSearchController:(UISearchController *)searchController { // 搜索指定内容的聊天记录 [ self .noResultView removeFromSuperview]; NSString * keyWord = searchController.searchBar.text; // 关键字 // NOTE: EMMessageSearchDirectionDown 向下搜索是搜索比你设置的timestamp晚的消息 // EMMessageSearchDirectionUp 向上搜索是搜索比你设置的timestamp早的消息 [ self .conversation loadMessagesWithKeyword:keyWord timestamp:[ self .searchTime timeIntervalSince1970] * 1000 count:SEARCHMESSAGE_PAGE_SIZE fromUser:@ "" searchDirection:EMMessageSearchDirectionDown completion:^( NSArray *aMessages, EMError *aError) { if (aMessages.count == 0) { [ self .view addSubview: self .noResultView]; } [_dataSourceArray removeAllObjects]; [_dataSourceArray addObjectsFromArray:aMessages]; [ self .tableView reloadData]; }]; } |
再给大家看一下截图,你在做的过程中要有什么问题,你也可以随时提出了!
最后一个说说这个清空聊天纪录,这里就简单了,也是SDK里面写好的东西,在这里就不花时间,给大家提一下,大家看看!在 SDK的 EMConversation 里面有这个方法 deleteAllMessages就可以把一个会话的消息给清空。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话