iOS 环信SDK-自定义扩展消息

iOS环信SDK(基于3.0)-自定义扩展消息-可携带用户头像与昵称等信息。


1,自定义扩展消息

在 EaseMessageViewController 中编辑。。。

NSDictionary *ext = @{@"name":@"name",

@"icon":@"http://icon.png",

@"other":@"。。。",};

EMTextMessageBody *msgB = [[EMTextMessageBody alloc] initWithText:@"扩展消息链接"];

NSString *from = [[EMClient sharedClient] currentUsername];

NSString *to = self.conversation.conversationId;

EMMessage *message = [[EMMessage alloc] initWithConversationID:to from:from to:to body:msgB ext:dict];

message.chatType = EMChatTypeChat; // 消息类型

message.ext = dict;

// 发送消息 (调用 EaseMessageViewController 中的方法)

[self _sendMessage:message];


2,文字消息-添加头像与昵称扩展

在 EaseMessageViewController 中编辑。。。

// 更改此方法

- (void)sendTextMessage:(NSString *)text withExt:(NSDictionary*)ext {

EMMessage *message = [EaseSDKHelper sendTextMessage:text to:self.conversation.conversationId messageType:[self _messageTypeFromConversationType] messageExt:ext];

//新增头像和昵称扩展

message.ext = @{@"name":@"name", @"icon":@"http://icon.png"};

[self _sendMessage:message];

}


*后记:重点在这里,我发现所有发送消息动作都会走 [self _sendMessage:message]; 方法,所以,在 [self _sendMessage:message]; 方法中只需加一次扩展消息就可以了,不用再像上面步骤多次粘贴复制。

message.ext = @{@"name":@"name", @"icon":@"http://icon.png"}; // ext就是扩展数据,_sendMessage方法在EaseMessageViewController中。。。


。。。发送其他种类的消息,扩展方法雷同。。。


下面是解析扩展消息:

在 EaseMessageViewController 中编辑。。。

在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 中编辑。。。

自定义扩展cell

1.

判断是否有扩展(或判断扩展中某个值来判断扩展消息类型)

if (model.message.ext && model.message.ext.count>2) 

2.

SDK自带的自定义cell - EaseCustomMessageCell

NSString *CellIdentifier = [EaseCustomMessageCell cellIdentifierWithModel:model];

EaseCustomMessageCell *cell = (EaseCustomMessageCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

cell = [[EaseCustomMessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier model:model];

cell.selectionStyle = UITableViewCellSelectionStyleNone;

}

NSDictionary *dict = model.message.ext;

model.avatarURLPath = [dict objectForKey:@"icon"];

model.nickname = [dict objectForKey:@"nickName"];

cell.customDelegate = self;

cell.model = model;

return cell;

默认消息cell

在 EaseBaseMessageCell 中编辑。。。

if (self.model.isSender) {

// kkk(kkUserIcon) UserDefault中头像

if (kkk(kkUserIcon)) {

[self.avatarView sd_setImageWithURL:[NSURL URLWithString:kkk(kkUserIcon)] placeholderImage:model.avatarImage];

} else {

self.avatarView.image = model.avatarImage;

}

}else {

if (model.avatarURLPath) {

[self.avatarView sd_setImageWithURL:[NSURL URLWithString:model.avatarURLPath] placeholderImage:model.avatarImage];

} else {

self.avatarView.image = model.avatarImage;

}

}

_nameLabel.text = model.nickname;


完成!2017.3.12

更改于-2017.12.29

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容