前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >环信SDK 头像、昵称、表情自定义和群聊设置的实现 二(附源码)

环信SDK 头像、昵称、表情自定义和群聊设置的实现 二(附源码)

作者头像
Mr.RisingSun
发布2018-02-06 11:18:33
1K0
发布2018-02-06 11:18:33
举报

前言:

环信SDK 头像、昵称、表情自定义和群聊设置的实现 一(附源码)

   接着上面说的,我们来说说表情,它在哪里可以自定义,怎么写,那个方法是添加表情的我们都说说,找到 ChatViewController.m文件,它里面有这个方法;

-(NSArray*)emotionFormessageViewController:(EaseMessageViewController *)viewController

你可以在这里方法里面添加自己的表情,先看看Demo里面的源码,上面有自己的一些注释;

-(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中有这样一个属性

/*!
 *  \~chinese 
 *  群组模块
 */
@property (nonatomic, strong, readonly) id<IEMGroupManager> groupManager;

 这个属性可以用来管理屏蔽等这些属于群设置的内容,你通过 [EMClient sharedClient].groupManager 就可以取到它了,然后剩下的就是按部就班的操作,给大家随便写一个,比如下面这是一个完整的屏蔽群消息和取消的方法,说下面三点:

一:群主是不能屏蔽群消息的。

二:屏蔽了群消息你记得还有关闭接受并提醒这个操作,因为你屏蔽之后也就相应的收不到消息了。

三:刚进入这个设置界面,你得判断你是不是屏蔽了这个群的消息,用的就是  isBlocked 的属性,下面的接收提醒的是一个道理,刚进来都要判断你是不是开了!当然这些都是SDK里面的属性,你知道会用就行。

-(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里面有的,这里有几个地方你要注意的,我也在下面代码的注释里面有加,下面这个方法是当上面的搜索框一档输入的内容有变化就执行一次:

#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就可以把一个会话的消息给清空。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-02-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档