iOS 环信easeUI实现头像昵称

1、在会话(EaseConversationListViewController)列表进入聊天界面时,头像和昵称的处理。首先要获取到对方发送的扩展信息ext,然后通过字典 好友的ID作为key存在本地。


13CF2825-D042-4A2E-9831-1649615AD568.png
        NSLog(@"获取扩展信息 -- %@",model.conversation.lastReceivedMessage.ext);
        NSLog(@"好友的ID----%@",model.conversation.conversationId);
        
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:model.conversation.lastReceivedMessage.ext];
        
        NSDictionary *otherInfo = @{@"avatar":dic[@"avatar"],@"nickname":dic[@"nick"]};
        [NSUSERDEFAULTS setObject:otherInfo forKey: model.conversation.conversationId];
        [NSUSERDEFAULTS synchronize];

2、在聊天类(EaseMessageViewController)对扩展信息进行处理。
// (1) 定义全局属性

    NSString *friendMsgHdImvUrl;//对方 - 发送信息的 - 头像 - 下载链接
    NSString *friendMsgNickName;//对方 - 发送信息的 - 昵称

(2)处理信息

iOS 环信easeUI实现头像昵称_第1张图片
CB8C1482-C53C-4FA9-B11A-93F0158E21A6.png
-(void)setNavTitle:(NSString *)title
{
    NSDictionary *InfoDic = [NSUSERDEFAULTS valueForKey:title];
    NSLog(@"信息:%@",InfoDic);
    friendMsgHdImvUrl = InfoDic[@"avatar"];//对方 - 发送信息的 - 头像 - 下载链接
    friendMsgNickName = InfoDic[@"nickname"];//对方 - 发送信息的 - 昵称
    self.title = friendMsgNickName;
}

(2)- (NSArray *)formatMessages:(NSArray *)messages的方法处理

#pragma mark - public
- (NSArray *)formatMessages:(NSArray *)messages
{
   
    
    NSMutableArray *formattedArray = [[NSMutableArray alloc] init];
    if ([messages count] == 0) {
        return formattedArray;
    }
    
    for (EMMessage *message in messages) {
        //Calculate time interval
        CGFloat interval = (self.messageTimeIntervalTag - message.timestamp) / 1000;
        if (self.messageTimeIntervalTag < 0 || interval > 60 || interval < -60) {
            NSDate *messageDate = [NSDate dateWithTimeIntervalInMilliSecondSince1970:(NSTimeInterval)message.timestamp];
            NSString *timeStr = @"";
            
            if (_dataSource && [_dataSource respondsToSelector:@selector(messageViewController:stringForDate:)]) {
                timeStr = [_dataSource messageViewController:self stringForDate:messageDate];
            }
            else{
                timeStr = [messageDate formattedTime];
            }
            [formattedArray addObject:timeStr];
            self.messageTimeIntervalTag = message.timestamp;
        }
        
        //Construct message model
        id model = nil;
        if (_dataSource && [_dataSource respondsToSelector:@selector(messageViewController:modelForMessage:)]) {
            model = [_dataSource messageViewController:self modelForMessage:message];
            
        }
        else{
            model = [[EaseMessageModel alloc] initWithMessage:message];
            NSString *loginUserName = [[EMClient sharedClient] currentUsername];
            if ([model.message.from isEqualToString:loginUserName]) {
                // model.avatarImage = [UIImage imageNamed:@"EaseUIResource.bundle/user"];
                model.avatarURLPath = [NSUSERDEFAULTS valueForKey:kUserIcon];
                model.failImageName = @"imageDownloadFail";
                model.nickname = [NSString stringWithFormat:@"%@",[NSUSERDEFAULTS valueForKey:kUserName]] ;
            }
            
            else{
                
                NSString *avatarURLPathStr =friendMsgHdImvUrl;
                
                model.failImageName = @"imageDownloadFail";
                
                
                if (avatarURLPathStr == nil ||avatarURLPathStr == NULL) {
                    model.avatarImage = [UIImage imageNamed:@"EaseUIResource.bundle/user"];
                    
                    
                }else{
                    model.avatarURLPath = friendMsgHdImvUrl;
                }
                NSString *nicknameStr = friendMsgNickName;
                NSLog(@"2昵称 --- %@",nicknameStr);
                if (nicknameStr == nil || nicknameStr == NULL) {
                    
                }else{
                    model.nickname =[NSString stringWithFormat:@"%@",friendMsgNickName];
                }
                
            }
            
        }
        
        if (model) {
            [formattedArray addObject:model];
        }
    }
    
    return formattedArray;
}

3、最后一步,就是在你发送信息的时候,也要把您的头像和昵称 放在扩展信息li;- (void)_sendMessage:(EMMessage *)message的方法里添加一下代码。

    //lxq添加消息扩展(传送头像 和 昵称)
    NSMutableDictionary *dictExt = [NSMutableDictionary dictionaryWithDictionary:message.ext];
    [dictExt setValue:[NSUSERDEFAULTS objectForKey:kUserName] forKey:@"nick"];
    [dictExt setValue:[NSUSERDEFAULTS objectForKey:kUserIcon] forKey:@"avatar"];
    message.ext = dictExt;

你可能感兴趣的:(iOS 环信easeUI实现头像昵称)