iOS 环信集成通过对消息扩展实现头像和昵称的显示

前提把环信SDK和EaseUI成功集成后,我集成的环信SDK版本是2.x,EaseUI版本是3.x,效果图如下:

iOS 环信集成通过对消息扩展实现头像和昵称的显示_第1张图片
B8E912F7B8310F96A1FD2EFC81F12769.png

在继承自EaseMessageViewController的类里对消息进行扩展

  • - (void)sendTextMessage:(NSString )text withExt:(NSDictionary)ext
    其实我们需要的正是这个方法,对这个方法进行重写就OK了,可是环信却把它私有化了,于是只能按着下面的的方法进行消息扩展了,第一部分是EaseUI开源的方法,第二部分是我重写的方法

1、 环信EaseUI提供的API的实现

- (void)sendTextMessage:(NSString *)text
{
    [self sendTextMessage:text withExt:nil];
}

- (void)sendTextMessage:(NSString *)text withExt:(NSDictionary*)ext
{
    EMMessage *message = [EaseSDKHelper sendTextMessage:text
                                                   to:self.conversation.conversationId
                                          messageType:[self _messageTypeFromConversationType]
                                           messageExt:ext];
    [self _sendMessage:message];
}

2、对环信得方法进行重写

#pragma mark - EaseMessageViewControllerDataSource

- (id)messageViewController:(EaseMessageViewController *)viewController
                           modelForMessage:(EMMessage *)message
{
    id model = nil;
    
    model = [[EaseMessageModel alloc] initWithMessage:message];
    if (model.isSender) {
        NSLog(@"自己发送");
        
        model.message.ext = @{@"avatar":@"http://7xrpiy.com1.z0.glb.clouddn.com/babyShow/1462945613-0",@"nick":@"自己本人的昵称"};
        //头像
        //NSLog(@"***++++**%@",model.message);
        model.avatarURLPath = @"http://7xrpiy.com1.z0.glb.clouddn.com/babyShow/1462945613-0";
        //NSLog(@"******%@",model.avatarURLPath);
        //昵称
        model.nickname = @"自己的名字";
        //头像占位图
        model.failImageName = @"sunlei.jpg";
        
    }else{
        NSLog(@"对方发送");
        //头像
        model.avatarURLPath = model.message.ext[@"avatar"];
        //NSLog(@"+++++++______+++%@",model.avatarURLPath);
        //昵称
        model.nickname = model.message.ext[@"nick"];
        //头像占位图
        model.failImageName = @"sunlei.jpg";
        
    }
    //NSLog(@"+++++++++++%@",model.message);
    return model;
}

#pragma mark - 发送文本消息,对消息进行扩展
- (void)sendTextMessage:(NSString *)textMessage{
    NSLog(@"发送文本消息");
    NSDictionary *ext =  @{@"avatar":@"http://7xrpiy.com1.z0.glb.clouddn.com/babyShow/1462945613-0",@"nick":@"自己本人的昵称"};;
    
    
    EMMessage *message = [EaseSDKHelper sendTextMessage:textMessage
                                                     to:self.conversation.chatter
                                            messageType:eMessageTypeChatRoom
                                      requireEncryption:NO
                                             messageExt:ext];
    
    [self addMessageToDataSource:message
                        progress:nil];
}

#pragma mark - 发送图片消息

- (void)sendImageMessage:(UIImage *)image{
    NSLog(@"发送图片消息");
    NSDictionary *ext =  @{@"avatar":@"http://7xrpiy.com1.z0.glb.clouddn.com/babyShow/1462945613-0",@"nick":@"自己本人的昵称"};;

    EMMessage *message = [EaseSDKHelper sendImageMessageWithImage:image
                                                               to:self.conversation.chatter
                                                      messageType:eMessageTypeChatRoom
                                                requireEncryption:NO
                                                       messageExt:ext
                                                         progress:nil];
    
    [self addMessageToDataSource:message
                        progress:nil];
    
}

#pragma mark - 发送位置消息
- (void)sendLocationMessageLatitude:(double)latitude
                          longitude:(double)longitude
                         andAddress:(NSString *)address{
    NSLog(@"发送位置消息");
    NSDictionary *ext =  @{@"avatar":@"http://7xrpiy.com1.z0.glb.clouddn.com/babyShow/1462945613-0",@"nick":@"自己本人的昵称"};;
    
    EMMessage *message = [EaseSDKHelper sendLocationMessageWithLatitude:latitude
                                                              longitude:longitude
                                                                address:address
                                                                     to:self.conversation.chatter
                                                            messageType:eMessageTypeChatRoom
                                                      requireEncryption:NO
                                                             messageExt:ext];
    
    [self addMessageToDataSource:message
                        progress:nil];
    
    
}

#pragma mark - 发送语音消息
- (void)sendVoiceMessageWithLocalPath:(NSString *)localPath
                             duration:(NSInteger)duration{
    NSLog(@"发送语音消息");
    NSDictionary *ext =  @{@"avatar":@"http://7xrpiy.com1.z0.glb.clouddn.com/babyShow/1462945613-0",@"nick":@"自己本人的昵称"};;
    
    EMMessage *message = [EaseSDKHelper sendVoiceMessageWithLocalPath:localPath
                                                             duration:duration
                                                                   to:self.conversation.chatter
                                                          messageType:eMessageTypeChatRoom
                                                    requireEncryption:NO
                                                           messageExt:ext
                                                             progress:nil];
    
    [self addMessageToDataSource:message
                        progress:nil];
}

#pragma mark - 发送视频消息
- (void)sendVideoMessageWithURL:(NSURL *)url{
    NSLog(@"发送视频消息");
    NSDictionary *ext =  @{@"avatar":@"http://7xrpiy.com1.z0.glb.clouddn.com/babyShow/1462945613-0",@"nick":@"自己本人的昵称"};;
    
    EMMessage *message = [EaseSDKHelper sendVideoMessageWithURL:url
                                                             to:self.conversation.chatter
                                                    messageType:eMessageTypeChatRoom
                                              requireEncryption:NO
                                                     messageExt:ext
                                                       progress:nil];
    
    [self addMessageToDataSource:message
                        progress:nil];
}

你可能感兴趣的:(iOS 环信集成通过对消息扩展实现头像和昵称的显示)