iOS環(huán)信集成單聊和會話列表并修改頭像與昵稱和對消息內(nèi)容修改

一 : 集成單聊

1.AppDelegate.m

#import <HyphenateLite/HyphenateLite.h>

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {

EMOptions *options = [EMOptions optionsWithAppkey:@"*********"];//環(huán)信的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.你想實現(xiàn)單聊的類里,你可以繼承EaseMessageViewController,也可以直接使用EaseMessageViewController

?- (void)btnClick{

? ? EaseMessageViewController *chatController = [[EaseMessageViewController alloc] initWithConversationChatter:@"8003" conversationType:EMConversationTypeChat];

?? // chatController.title = @"8003";

? ? [self.navigationController pushViewController:chatController animated:YES];

}

到這里單聊的功能就完成了

二 : 修改頭像和昵稱并修改消息內(nèi)容

?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) {//自己發(fā)送

? ? ? ? model.message.ext=@{@"avatar":headUrlImage,@"nickname":bianNickName};

?? ? ? // 頭像

? ? ? ? model.avatarURLPath= headUrlImage;

? ? ? ? //昵稱

? ? ? ? model.nickname= bianNickName;

? ? ? ? //頭像占位圖

? ? ? ? model.avatarImage= [UIImageimageNamed:@"imageDownloadFail"];

? ? }else{//對方發(fā)送

? ? ? ? //頭像占位圖

? ? ? ? model.avatarImage= [UIImageimageNamed:@"imageDownloadFail"];

? ? ? ? //頭像

? ? ? ? model.avatarURLPath= message.ext[@"avatar"];

? ? ? ? //昵稱

? ? ? ? model.nickname=? message.ext[@"nickname"];

? ? }

? ? returnmodel;

}

第二個地方修改下面方法里增加備注內(nèi)容


- (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];

? ? ? ? ? ? }

? ? ? ? }];

? ? }

}

第三個地方修改下面方法里增加備注內(nèi)容


- (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;

}

到此修改頭像和昵稱已全部完成

下面是修改消息內(nèi)容


增加修改的部分

- (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"];

//所有消息發(fā)送的數(shù)字都會轉(zhuǎn)變?yōu)?

? ? 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;

}

三 : 修改會話列表的頭像和昵稱

創(chuàng)建一個類集成與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_免費高速下載|百度網(wǎng)盤-分享無限制

到此所有修改結(jié)束,上述僅代表本人個人操作集成時所遇到問題解決辦法

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容