詳解iOS集成融云SDK即時(shí)通訊整理
最近很少寫一下項(xiàng)目總結(jié)了,最近項(xiàng)目雖然做了很多,但是都是一些外包項(xiàng)目,做下來也沒有什么值得總結(jié)的。最近一個(gè)項(xiàng)目用到了融云即時(shí)通訊,以前基本都是用環(huán)信,所以還遇到了一些問題,在此總結(jié)一下記錄一下。
1 頭像、昵稱等用戶信息(融云對這個(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ù)存儲聯(lián)系人信息的模型,通過 userId 來取得對應(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)然我覺得還可以從后臺獲取好友關(guān)系后,我們在每次登陸后,開一個(gè)線程把好友關(guān)系請求下來存起來然后根據(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 = @"請輸入文字信息..."; _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 取消輸入@彈出好友列表界面,保留長按頭像@方法
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)
// 對自定義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對應(yīng)的model,然后從model中拿到對應(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添加對應(yīng)類型的聊天就可以獲取對應(yīng)類型的聊天列表刪除方法類似
[[RCIMClient sharedRCIMClient] clearConversations:@[@(ConversationType_PRIVATE)]];
6 背景圖
融云聊天列表沒有數(shù)據(jù)的默認(rèn)圖片下面有點(diǎn)擊右上角加入聊天,可是不是所有的聊天都有這個(gè)功能(我的就沒有)如何沒有就可以在資源文件中找到 no_message_img 這張圖片用ps去掉下面的那一行字😆
7 其它
以上就是我在使用融云過程中遇到的一些問題及解決方法,如果有錯(cuò)誤或者不足之處還望指正,謝謝!也希望大家多多支持腳本之家。
相關(guān)文章
iOS動(dòng)畫教你編寫Slack的Loading動(dòng)畫進(jìn)階篇
這篇文章主要為大家進(jìn)一步詳細(xì)介紹了iOS動(dòng)畫教你編寫Slack的Loading動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
iOS開發(fā)retina屏幕下的點(diǎn)與像素關(guān)系詳解
這篇文章主要為大家介紹了iOS開發(fā)retina屏幕下的點(diǎn)與像素關(guān)系詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
總結(jié)iOS實(shí)現(xiàn)漸變顏色的三種方法
這篇文章主要給大家總結(jié)了iOS實(shí)現(xiàn)漸變顏色的三種方法,分別是利用CAGradientLayer實(shí)現(xiàn)漸變、Core Graphics相關(guān)方法實(shí)現(xiàn)漸變以及用CAShapeLayer作為layer的mask屬性實(shí)現(xiàn),大家可以根據(jù)自己的需要選擇使用,下面來一起看看吧。2016-10-10
IOS開發(fā)代碼分享之用nstimer實(shí)現(xiàn)倒計(jì)時(shí)功能
在制作IOS項(xiàng)目中,我們經(jīng)常要用到倒計(jì)時(shí)功能,今天就分享下使用nstimer實(shí)現(xiàn)的倒計(jì)時(shí)功能的代碼,希望對大家能有所幫助2014-09-09
iOS 圖片上傳使用base64或者二進(jìn)制流上傳頭像功能
這篇文章主要介紹了iOS 圖片上傳使用base64或者二進(jìn)制流上傳頭像功能,需要的朋友可以參考下2017-09-09
實(shí)例解析iOS應(yīng)用多線程開發(fā)中NSthread類的用法
這篇文章主要介紹了iOS應(yīng)用多線程開發(fā)中NSthread類的用法,代碼基于傳統(tǒng)的Objective-C,NSthread類需要的朋友可以參考下2016-02-02
iOS實(shí)現(xiàn)通過按鈕添加和刪除控件的方法
這篇文章主要為大家詳細(xì)介紹了iOS通過按鈕添加和刪除控件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
iOS滑動(dòng)全屏實(shí)現(xiàn)返回功能
這篇文章主要為大家詳細(xì)介紹了iOS滑動(dòng)全屏實(shí)現(xiàn)返回功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05

