環信SDK 頭像、昵稱、表情自定義和群聊設置的實現 二(附源碼)


前言:

        環信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就可以把一個會話的消息給清空。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM