詳解iOS集成融云SDK即時(shí)通訊整理
最近很少寫(xiě)一下項(xiàng)目總結(jié)了,最近項(xiàng)目雖然做了很多,但是都是一些外包項(xiàng)目,做下來(lái)也沒(méi)有什么值得總結(jié)的。最近一個(gè)項(xiàng)目用到了融云即時(shí)通訊,以前基本都是用環(huán)信,所以還遇到了一些問(wèn)題,在此總結(jié)一下記錄一下。
1 頭像、昵稱(chēng)等用戶信息(融云對(duì)這個(gè)問(wèn)題有兩種處理方式)
1.用戶信息提供者
實(shí)現(xiàn)步驟(以下代碼放在單例中,可以是AppDelegate,最好單獨(dú)寫(xiě)一個(gè)單例)
首先遵守RCIMUserInfoDataSource這個(gè)協(xié)議
然后是要設(shè)置代理
[[RCIM sharedRCIM] setUserInfoDataSource:self];
最后實(shí)現(xiàn)代理方法:
- (void)getUserInfoWithUserId:(NSString *)userId completion:(void (^)(RCUserInfo *))completion { NSLog(@"getUserInfoWithUserId ----- %@", userId); RCUserInfo *user = [RCUserInfo new]; if (userId == nil || [userId length] == 0) { user.userId = userId; user.portraitUri = @""; user.name = @""; completion(user); return; } if ([userId isEqualToString:[UserInfo shareInstance].uid]) { NSString *urlSelf = [BASIC_URL_image stringByAppendingString:[UserInfo shareInstance].photo]; return completion([[RCUserInfo alloc] initWithUserId:userId name:[UserInfo shareInstance].nickname portrait:urlSelf]); }else { //根據(jù)存儲(chǔ)聯(lián)系人信息的模型,通過(guò) userId 來(lái)取得對(duì)應(yīng)的name和頭像url,進(jìn)行以下設(shè)置 [WTBaseHttpRequst postRequstWithURL:getUserHttp params:@{@"uid":[UserInfo shareInstance].uid, @"api_token":[UserInfo shareInstance].api_token, @"k_uid":userId} successBlock:^(NSDictionary *returnData) { if ([returnData[@"status"] integerValue] == 1) { NSString *urlStr = [BASIC_URL_image stringByAppendingString:returnData[@"data"][@"user"][@"photo"]]; return completion([[RCUserInfo alloc] initWithUserId:userId name:returnData[@"data"][@"user"][@"nickname"] portrait:urlStr]); }else { completion(user); } } failureBlock:^(NSString *error) { completion(user); } showHUD:NO]; } }
這個(gè)方法不需要你自己手動(dòng)調(diào)用,只是當(dāng)你在修改用戶信息時(shí)調(diào)用
[[RCIM sharedRCIM] refreshUserInfoCache:user withUserId:[UserInfo shareInstance].uid]
方法即可
WS(weakSelf); // 修改用戶信息調(diào)用 [WTBaseHttpRequst postRequstWithURL:modifyInfoHttp params:dict successBlock:^(NSDictionary *returnData) { [weakSelf MBProgressHudShowWithTextOnlyWithText:returnData[@"msg"]]; if ([returnData[@"status"] integerValue] == 1) { RCUserInfo *user = [RCUserInfo new]; user.userId = [UserInfo shareInstance].uid; user.portraitUri = [BASIC_URL_image stringByAppendingString:[UserInfo shareInstance].photo]; user.name = weakSelf.nickNameTextField.text; [[RCIM sharedRCIM] refreshUserInfoCache:user withUserId:[UserInfo shareInstance].uid]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self.navigationController popViewControllerAnimated:YES]; }); } } failureBlock:^(NSString *error) { [weakSelf MBProgressHudShowWithTextOnlyWithText:error]; } showHUD:YES];
2.在擴(kuò)展消息中攜帶用戶信息
設(shè)置發(fā)送消息時(shí)在消息體中攜帶用戶信息(從2.4.1 之后附加用戶信息之后cell默認(rèn)會(huì)顯示附加的用戶信息的頭像,即用戶信息不會(huì)取用戶信息提供者里提供的用戶信息)
[RCIM sharedRCIM].enableMessageAttachUserInfo = YES;
你設(shè)置了enableMessageAttachUserInfo之后,可以取到
/** * 發(fā)送者信息 * **/ @property(nonatomic, strong) RCUserInfo *senderUserInfo;
當(dāng)然我覺(jué)得還可以從后臺(tái)獲取好友關(guān)系后,我們?cè)诿看蔚顷懞螅_(kāi)一個(gè)線程把好友關(guān)系請(qǐng)求下來(lái)存起來(lái)然后根據(jù)環(huán)信ID查找好友的昵稱(chēng)和頭像
2 給輸入框添加提示語(yǔ)(這個(gè)我一直覺(jué)得環(huán)信應(yīng)該給了方法修改,只是我一直沒(méi)有找到這個(gè)方法,所以只有自己去寫(xiě)了)
1.創(chuàng)建提示的label
_lab = [[UILabel alloc] initWithFrame:self.chatSessionInputBarControl.inputTextView.bounds]; _lab.text = @"請(qǐng)輸入文字信息..."; _lab.textColor = [UIColor colorWithHexColor:@"dddddd"]; _lab.font = [UIFont systemFontOfSize:15]; _lab.center = CGPointMake(_lab.center.x + 15, _lab.center.y);
2.判定是否有草稿來(lái)顯示和隱藏提示的label
[self.chatSessionInputBarControl.inputTextView addSubview:_lab]; if (self.chatSessionInputBarControl.draft == nil || self.chatSessionInputBarControl.draft.length == 0) { _lab.hidden = NO; }else { _lab.hidden = YES; }
3.根據(jù)輸入數(shù)據(jù)來(lái)判定顯示隱藏提示label
- (void)inputTextView:(UITextView *)inputTextView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if (((inputTextView.text.length == 1 && [text isEqualToString:@""]) || (inputTextView.text.length == 0 && text.length > 0)) && range.length == 1 && range.location == 0) { _lab.hidden = NO; }else { _lab.hidden = YES; } }
3 取消輸入@彈出好友列表界面,保留長(zhǎng)按頭像@方法
1.首先在AppDelegate中開(kāi)啟消息@功能(只支持群聊和討論組, App需要實(shí)現(xiàn)群成員數(shù)據(jù)源groupMemberDataSource)
[RCIM sharedRCIM].enableMessageMentioned = YES;
然后在繼承RCConversationViewController的控制器中調(diào)用
-(void)showChooseUserViewController:(void (^)(RCUserInfo *selectedUserInfo))selectedBlock cancel:(void (^)())cancelBlock { }
4 在會(huì)話列表中添加一些固定的cell(繼承RCConversationListViewController)
// 對(duì)自定義cell賦值 - (RCConversationBaseCell *)rcConversationListTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { RCCustomCell *cell = (RCCustomCell *)[[RCCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RCCustomCell"]; RCConversationModel *model = self.conversationListDataSource[indexPath.row]; cell.nameLabel.text = model.conversationTitle; return cell; } // 添加自定義cell的數(shù)據(jù)源 - (NSMutableArray *)willReloadTableData:(NSMutableArray *)dataSource{ NSArray *arr = @[@"論壇回復(fù)和@我的", @"陌生人私信", @"幸存者部落@我的", @"問(wèn)卷調(diào)查"]; for (int i = 0; i<arr.count; i++) { RCConversationModel *model = [[RCConversationModel alloc]init]; model.conversationModelType = RC_CONVERSATION_MODEL_TYPE_CUSTOMIZATION; model.conversationTitle = arr[i]; model.isTop = YES; [dataSource insertObject:model atIndex:i]; } return dataSource; } // 點(diǎn)擊cell跳轉(zhuǎn) - (void)onSelectedTableRow:(RCConversationModelType)conversationModelType conversationModel:(RCConversationModel *)model atIndexPath:(NSIndexPath *)indexPath{ if (indexPath.row == 0) { WTForumAndConnectListViewController *chatList = (WTForumAndConnectListViewController *)[WTStoryBoardSegment instantiateViewControllerWithStoryBoardName:@"Main" identifier:@"WTForumAndConnectListViewController"]; chatList.title = @"回復(fù)和@我的"; [self.navigationController pushViewController:chatList animated:YES]; }else if (indexPath.row == 1) { WTChatListViewController *chatList = [[WTChatListViewController alloc] init]; chatList.title = @"陌生人私信"; chatList.isEnteredToCollectionViewController = YES; chatList.type = 1; chatList.friendArray = self.friendArray; [self.navigationController pushViewController:chatList animated:YES]; }else if (indexPath.row == 2) { WTChatListViewController *chatList = [[WTChatListViewController alloc] init]; chatList.title = @"幸存者部落@我的"; chatList.isEnteredToCollectionViewController = YES; chatList.type = 2; [self.navigationController pushViewController:chatList animated:YES]; }else if (indexPath.row == 3) { WTQuestionnaireViewController *questionnaire = (WTQuestionnaireViewController *)[WTStoryBoardSegment instantiateViewControllerWithStoryBoardName:@"Main" identifier:@"WTQuestionnaireViewController"]; [self.navigationController pushViewController:questionnaire animated:YES]; }else { //點(diǎn)擊cell,拿到cell對(duì)應(yīng)的model,然后從model中拿到對(duì)應(yīng)的RCUserInfo,然后賦值會(huì)話屬性,進(jìn)入會(huì)話 if (model.conversationType == ConversationType_PRIVATE) {//單聊 WTMyConversationLisViewController *_conversationVC = [[WTMyConversationLisViewController alloc]init]; _conversationVC.conversationType = model.conversationType; _conversationVC.targetId = model.targetId; _conversationVC.title = model.conversationTitle; [self.navigationController pushViewController:_conversationVC animated:YES]; }else if (model.conversationType == ConversationType_GROUP){//群聊 WTMyConversationLisViewController *_conversationVC = [[WTMyConversationLisViewController alloc]init]; _conversationVC.conversationType = model.conversationType; _conversationVC.title = model.conversationTitle; _conversationVC.targetId = model.targetId; [self.navigationController pushViewController:_conversationVC animated:YES]; } } }
5 在任意地方獲取聊天列表數(shù)量及刪除列表
獲取聊天列表
NSArray *privateArr = [[RCIMClient sharedRCIMClient] getConversationList:@[@(ConversationType_PRIVATE)]];
在ConversationList添加對(duì)應(yīng)類(lèi)型的聊天就可以獲取對(duì)應(yīng)類(lèi)型的聊天列表刪除方法類(lèi)似
[[RCIMClient sharedRCIMClient] clearConversations:@[@(ConversationType_PRIVATE)]];
6 背景圖
融云聊天列表沒(méi)有數(shù)據(jù)的默認(rèn)圖片下面有點(diǎn)擊右上角加入聊天,可是不是所有的聊天都有這個(gè)功能(我的就沒(méi)有)如何沒(méi)有就可以在資源文件中找到 no_message_img 這張圖片用ps去掉下面的那一行字😆
7 其它
以上就是我在使用融云過(guò)程中遇到的一些問(wèn)題及解決方法,如果有錯(cuò)誤或者不足之處還望指正,謝謝!也希望大家多多支持腳本之家。
相關(guān)文章
iOS實(shí)現(xiàn)H5支付(微信、支付寶)原生封裝
這篇文章主要介紹了iOS實(shí)現(xiàn)H5支付(微信、支付寶)原生封裝,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02iOS開(kāi)發(fā)中導(dǎo)航控制器的基本使用教程
這篇文章主要介紹了iOS開(kāi)發(fā)中導(dǎo)航控制器的基本使用教程,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-11-11iOS 11 UINavigationItem 去除左右間隙的方法
本篇文章主要介紹了iOS 11 UINavigationItem 去除左右間隙的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10仿IOS效果 帶彈簧動(dòng)畫(huà)的ListView
這篇文章主要介紹了仿IOS效果,帶彈簧動(dòng)畫(huà)的ListView,感興趣的小伙伴們可以參考一下2016-01-01iOS實(shí)現(xiàn)毛玻璃效果(無(wú)需要第三方)
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)毛玻璃效果,無(wú)需要第三方,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05iOS App開(kāi)發(fā)中擴(kuò)展RCLabel組件進(jìn)行基于HTML的文本布局
RCLabel組件基于CoreText框架,可以將HTML標(biāo)記的文本內(nèi)容轉(zhuǎn)為富文本視圖,這里我們就來(lái)解讀如何在iOS App開(kāi)發(fā)中擴(kuò)展RCLabel組件進(jìn)行基于HTML的文本布局:2016-07-07iOS實(shí)現(xiàn)封裝一個(gè)獲取通訊錄的工具類(lèi)詳解
這篇文章主要給大家介紹了關(guān)于iOS如何實(shí)現(xiàn)封裝一個(gè)獲取通訊錄的工具類(lèi)的相關(guān)資料,這是自己平時(shí)封裝的一個(gè)工具類(lèi),使用非常方便,文中給出了詳細(xì)的示例代碼,需要的朋友們可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10