一 : 集成单聊
1.AppDelegate.m
#import <HyphenateLite/HyphenateLite.h>
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
EMOptions *options = [EMOptions optionsWithAppkey:@"*********"];//环信的Appkey
options.apnsCertName = @"istore_dev";//推送证书
[[EMClient sharedClient] initializeSDKWithOptions:options];
//注册
EMError *error = [[EMClient sharedClient] registerWithUsername:@"8001" password:@"111111"];
if (error==nil) {
NSLog(@"注册成功");
}
//登录
EMError *error4 = [[EMClient sharedClient] loginWithUsername:@"8001" password:@"111111"];
if(!error4) {
NSLog(@"登录成功");
}
}
- (void)applicationDidEnterBackground:(UIApplication*)application {
[[EMClient sharedClient] applicationDidEnterBackground:application];
}
- (void)applicationWillEnterForeground:(UIApplication*)application {
[[EMClient sharedClient] applicationWillEnterForeground:application];
}
2.你想实现单聊的类里,你可以继承EaseMessageViewController,也可以直接使用EaseMessageViewController
- (void)btnClick{
EaseMessageViewController *chatController = [[EaseMessageViewController alloc] initWithConversationChatter:@"8003" conversationType:EMConversationTypeChat];
// chatController.title = @"8003";
[self.navigationController pushViewController:chatController animated:YES];
}
到这里单聊的功能就完成了
二 : 修改头像和昵称并修改消息内容
1.修改头像和昵称需要修改三个地方
第一个地方,添加这方法
- (id)messageViewController:(EaseMessageViewController*)viewController
modelForMessage:(EMMessage*)message{
id model =nil;
model = [[EaseMessageModelalloc]initWithMessage:message];
//这两个是把自己的头像和昵称保存在了本地,这里取出来使用
NSString *headUrlImage = [[NSUserDefaults standardUserDefaults]objectForKey:@"headUrlImage"];
NSString *bianNickName = [[NSUserDefaults standardUserDefaults]objectForKey:@"nickName"];
if(model.isSender) {//自己发送
model.message.ext=@{@"avatar":headUrlImage,@"nickname":bianNickName};
// 头像
model.avatarURLPath= headUrlImage;
//昵称
model.nickname= bianNickName;
//头像占位图
model.avatarImage= [UIImageimageNamed:@"imageDownloadFail"];
}else{//对方发送
//头像占位图
model.avatarImage= [UIImageimageNamed:@"imageDownloadFail"];
//头像
model.avatarURLPath= message.ext[@"avatar"];
//昵称
model.nickname= message.ext[@"nickname"];
}
returnmodel;
}
第二个地方修改下面方法里增加备注内容
- (void)sendMessage:(EMMessage*)message isNeedUploadFile:(BOOL)isUploadFile
{
if (self.conversation.type == EMConversationTypeGroupChat){
message.chatType=EMChatTypeGroupChat;
}
else if (self.conversation.type == EMConversationTypeChatRoom){
message.chatType=EMChatTypeChatRoom;
}
/****************下面为修改部分********************/
NSString *headUrlImage = [[NSUserDefaults standardUserDefaults]objectForKey:@"headUrlImage"];
NSString *bianNickName = [[NSUserDefaults standardUserDefaults]objectForKey:@"nickName"];
NSDictionary*exit =@{@"avatar":headUrlImage,@"nickname":bianNickName};
message.ext= exit;
/****************上面为修改部分********************/
__weak typeof(self) weakself = self;
if (!([EMClient sharedClient].options.isAutoTransferMessageAttachments) && isUploadFile) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:NSLocalizedString(@"message.autoTransfer", @"Please customize the transfer attachment method") delegate:nil cancelButtonTitle:NSLocalizedString(@"sure", @"OK") otherButtonTitles:nil, nil];
[alertViewshow];
}else{
[self addMessageToDataSource:message
progress:nil];
[[EMClientsharedClient].chatManagersendMessage:messageprogress:^(intprogress) {
if(weakself.dataSource&& [weakself.dataSourcerespondsToSelector:@selector(messageViewController:updateProgress:messageModel:messageBody:)]) {
[weakself.dataSourcemessageViewController:weakselfupdateProgress:progressmessageModel:nilmessageBody:message.body];
}
}completion:^(EMMessage*aMessage,EMError*aError) {
if(!aError) {
[weakself_refreshAfterSentMessage:aMessage];
}
else{
[weakself.tableViewreloadData];
}
}];
}
}
第三个地方修改下面方法里增加备注内容
- (NSArray*)formatMessages:(NSArray*)messages
{
NSMutableArray*formattedArray = [[NSMutableArrayalloc]init];
if([messagescount] ==0) {
returnformattedArray;
}
for(EMMessage*messageinmessages) {
//Calculate time interval
CGFloatinterval = (self.messageTimeIntervalTag- message.timestamp) /1000;
if(self.messageTimeIntervalTag<0|| interval >60|| interval < -60) {
NSDate*messageDate = [NSDatedateWithTimeIntervalInMilliSecondSince1970:(NSTimeInterval)message.timestamp];
NSString*timeStr =@"";
if(_dataSource&& [_dataSourcerespondsToSelector:@selector(messageViewController:stringForDate:)]) {
timeStr = [_dataSourcemessageViewController:selfstringForDate:messageDate];
}
else{
timeStr = [messageDateformattedTime];
}
[formattedArrayaddObject:timeStr];
self.messageTimeIntervalTag = message.timestamp;
}
//Construct message model
id model =nil;
if(_dataSource&& [_dataSourcerespondsToSelector:@selector(messageViewController:modelForMessage:)]) {
model = [_dataSource messageViewController:self modelForMessage:message];
}
else{
// model = [[EaseMessageModel alloc] initWithMessage:message];
// model.avatarImage = [UIImage imageNamed:@"EaseUIResource.bundle/user"];
// model.failImageName = @"imageDownloadFail";
/****************下面为修改部分********************/
model = [[EaseMessageModelalloc]initWithMessage:message];
NSString*loginUserName = [[EMClientsharedClient]currentUsername];
NSString*headUrlImage = [[NSUserDefaultsstandardUserDefaults]objectForKey:@"headUrlImage"];
NSString*bianNickName = [[NSUserDefaultsstandardUserDefaults]objectForKey:@"nickName"];
if([model.message.fromisEqualToString:loginUserName]) {
model.avatarURLPath= headUrlImage;
model.failImageName=@"imageDownloadFail";
model.nickname= bianNickName;
}else{
model.avatarURLPath= message.ext[@"avatar"];
//昵称
model.nickname= message.ext[@"nickname"];
model.failImageName=@"imageDownloadFail";
}
/****************上面为修改部分********************/
}
if(model) {
[formattedArrayaddObject:model];
}
}
returnformattedArray;
}
到此修改头像和昵称已全部完成
下面是修改消息内容
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
idobject = [self.dataArrayobjectAtIndex:indexPath.row];
//time cell
if([objectisKindOfClass:[NSStringclass]]) {
NSString*TimeCellIdentifier = [EaseMessageTimeCellcellIdentifier];
EaseMessageTimeCell*timeCell = (EaseMessageTimeCell*)[tableViewdequeueReusableCellWithIdentifier:TimeCellIdentifier];
if(timeCell ==nil) {
timeCell = [[EaseMessageTimeCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TimeCellIdentifier];
timeCell.selectionStyle = UITableViewCellSelectionStyleNone;
}
timeCell.title= object;
returntimeCell;
}
id model = object;
if(_delegate&& [_delegaterespondsToSelector:@selector(messageViewController:cellForMessageModel:)]) {
UITableViewCell *cell = [_delegate messageViewController:tableView cellForMessageModel:model];
if(cell) {
if([cellisKindOfClass:[EaseMessageCellclass]]) {
EaseMessageCell*emcell= (EaseMessageCell*)cell;
if(emcell.delegate==nil) {
emcell.delegate=self;
}
}
returncell;
}
}
if(_dataSource&& [_dataSourcerespondsToSelector:@selector(isEmotionMessageFormessageViewController:messageModel:)]) {
BOOL flag = [_dataSource isEmotionMessageFormessageViewController:self messageModel:model];
if(flag) {
NSString*CellIdentifier = [EaseCustomMessageCellcellIdentifierWithModel:model];
//send cell
EaseCustomMessageCell*sendCell = (EaseCustomMessageCell*)[tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if(sendCell ==nil) {
sendCell = [[EaseCustomMessageCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifiermodel:model];
sendCell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if(_dataSource&& [_dataSourcerespondsToSelector:@selector(emotionURLFormessageViewController:messageModel:)]) {
EaseEmotion *emotion = [_dataSource emotionURLFormessageViewController:self messageModel:model];
if(emotion) {
model.image= [UIImagesd_animatedGIFNamed:emotion.emotionOriginal];
model.fileURLPath= emotion.emotionOriginalURL;
}
}
sendCell.model= model;
sendCell.delegate=self;
returnsendCell;
}
}
NSString*CellIdentifier = [EaseMessageCellcellIdentifierWithModel:model];
EaseBaseMessageCell*sendCell = (EaseBaseMessageCell*)[tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if(sendCell ==nil) {
sendCell = [[EaseBaseMessageCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifiermodel:model];
sendCell.selectionStyle = UITableViewCellSelectionStyleNone;
sendCell.delegate=self;
}
NSDictionary*dict = model.message.ext;
//model.avatarURLPath = [dict objectForKey:@"icon"];
// model.nickname = [dict objectForKey:@"nickName"];
//所有消息发送的数字都会转变为*
if(model.text==nil|| model.text==NULL|| [model.textisEqualToString:@"(null)"]) {
}else{
NSRegularExpression *regularExpression = [NSRegularExpression regularExpressionWithPattern:@"[0-9]" options:0 error:nil];
NSString *strText = [regularExpression stringByReplacingMatchesInString:model.text options:0 range:NSMakeRange(0, model.text.length) withTemplate:@"*"];
model.text = strText;
}
sendCell.model= model;
returnsendCell;
}
三 : 修改会话列表的头像和昵称
创建一个类集成与EaseConversationListViewController,在下面方法里做出修改即可
- (id)conversationListViewController:(EaseConversationListViewController*)conversationListViewController modelForConversation:(EMConversation*)conversation{
EaseConversationModel *model = [[EaseConversationModel alloc] initWithConversation:conversation];
if (model.conversation.type == EMConversationTypeChat) {
NSDictionary*dic = conversation.lastReceivedMessage.ext;
if(dic[@"nickname"] ==nil|| dic[@"avatar"] ==nil){
NSDictionary*dict = [[NSDictionaryalloc]init];
dict = [[NSUserDefaultsstandardUserDefaults]objectForKey:conversation.conversationId];
model.title= dict[@"nickname"];
model.avatarURLPath= dict[@"avatar"];
//头像占位图
model.avatarImage= [UIImageimageNamed:@"imageDownloadFail"];
}else{
model.title= dic[@"nickname"];
model.avatarURLPath= dic[@"avatar"];
//头像占位图
model.avatarImage= [UIImageimageNamed:@"imageDownloadFail"];
}
}else if (model.conversation.type == EMConversationTypeGroupChat) {
NSString*imageName =@"groupPublicHeader";
if(![conversation.extobjectForKey:@"subject"])
{
NSArray*groupArray = [[EMClientsharedClient].groupManagergetJoinedGroups];
for(EMGroup*groupingroupArray) {
if([group.groupIdisEqualToString:conversation.conversationId]) {
NSMutableDictionary*ext = [NSMutableDictionarydictionaryWithDictionary:conversation.ext];
[extsetObject:group.subjectforKey:@"subject"];
[extsetObject:[NSNumbernumberWithBool:group.isPublic]forKey:@"isPublic"];
conversation.ext= ext;
break;
}
}
}
NSDictionary*ext = conversation.ext;
model.title= [extobjectForKey:@"subject"];
imageName = [[extobjectForKey:@"isPublic"] boolValue] ? @"groupPublicHeader" : @"groupPrivateHeader";
model.avatarImage= [UIImageimageNamed:imageName];
//头像占位图
model.avatarImage= [UIImageimageNamed:@"imageDownloadFail"];
}
returnmodel;
}
这里是demo:HuanXinDemo.zip_免费高速下载|百度网盘-分享无限制
到此所有修改结束,上述仅代表本人个人操作集成时所遇到问题解决办法