最近很少寫一下項(xiàng)目總結(jié)了,最近項(xiàng)目雖然做了很多,但是都是一些外包項(xiàng)目,做下來也沒有什么值得總結(jié)的。最近一個(gè)項(xiàng)目用到了融云即時(shí)通訊,以前基本都是用環(huán)信,所以還遇到了一些問題,在此總結(jié)一下記錄一下。
1 頭像、昵稱等用戶信息(融云對(duì)這個(gè)問題有兩種處理方式)
1.用戶信息提供者
實(shí)現(xiàn)步驟(以下代碼放在單例中,可以是AppDelegate,最好單獨(dú)寫一個(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)系人信息的模型,通過 userId 來取得對(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)然我覺得還可以從后臺(tái)獲取好友關(guān)系后,我們?cè)诿看蔚顷懞螅_一個(gè)線程把好友關(guān)系請(qǐng)求下來存起來然后根據(jù)環(huán)信ID查找好友的昵稱和頭像
2 給輸入框添加提示語(這個(gè)我一直覺得環(huán)信應(yīng)該給了方法修改,只是我一直沒有找到這個(gè)方法,所以只有自己去寫了)
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.判定是否有草稿來顯示和隱藏提示的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ù)來判定顯示隱藏提示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中開啟消息@功能(只支持群聊和討論組, 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ù)和@我的", @"陌生人私信", @"幸存者部落@我的", @"問卷調(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)類型的聊天就可以獲取對(duì)應(yīng)類型的聊天列表刪除方法類似
[[RCIMClient sharedRCIMClient] clearConversations:@[@(ConversationType_PRIVATE)]];
6 背景圖
融云聊天列表沒有數(shù)據(jù)的默認(rèn)圖片下面有點(diǎn)擊右上角加入聊天,可是不是所有的聊天都有這個(gè)功能(我的就沒有)如何沒有就可以在資源文件中找到 no_message_img 這張圖片用ps去掉下面的那一行字
注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到IOS開發(fā)頻道。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注