详解iOS集成融云SDK即时通讯整理
最近很少写一下项目总结了,最近项目虽然做了很多,但是都是一些外包项目,做下来也没有什么值得总结的。最近一个项目用到了融云即时通讯,以前基本都是用环信,所以还遇到了一些问题,在此总结一下记录一下。
1 头像、昵称等用户信息(融云对这个问题有两种处理方式)
1.用户信息提供者
实现步骤(以下代码放在单例中,可以是appdelegate,最好单独写一个单例)
首先遵守rcimuserinfodatasource这个协议
然后是要设置代理
[[rcim sharedrcim] setuserinfodatasource:self];
最后实现代理方法:
- (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 { //根据存储联系人信息的模型,通过 userid 来取得对应的name和头像url,进行以下设置 [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]; } }
这个方法不需要你自己手动调用,只是当你在修改用户信息时调用
[[rcim sharedrcim] refreshuserinfocache:user withuserid:[userinfo shareinstance].uid]
方法即可
ws(weakself); // 修改用户信息调用 [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.在扩展消息中携带用户信息
设置发送消息时在消息体中携带用户信息(从2.4.1 之后附加用户信息之后cell默认会显示附加的用户信息的头像,即用户信息不会取用户信息提供者里提供的用户信息)
[rcim sharedrcim].enablemessageattachuserinfo = yes;
你设置了enablemessageattachuserinfo之后,可以取到
/** * 发送者信息 * **/ @property(nonatomic, strong) rcuserinfo *senderuserinfo;
当然我觉得还可以从后台获取好友关系后,我们在每次登陆后,开一个线程把好友关系请求下来存起来然后根据环信id查找好友的昵称和头像
2 给输入框添加提示语(这个我一直觉得环信应该给了方法修改,只是我一直没有找到这个方法,所以只有自己去写了)
1.创建提示的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.根据输入数据来判定显示隐藏提示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需要实现群成员数据源groupmemberdatasource)
[rcim sharedrcim].enablemessagementioned = yes;
然后在继承rcconversationviewcontroller的控制器中调用
-(void)showchooseuserviewcontroller:(void (^)(rcuserinfo *selecteduserinfo))selectedblock cancel:(void (^)())cancelblock { }
4 在会话列表中添加一些固定的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的数据源 - (nsmutablearray *)willreloadtabledata:(nsmutablearray *)datasource{ nsarray *arr = @[@"论坛回复和@我的", @"陌生人私信", @"幸存者部落@我的", @"问卷调查"]; 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; } // 点击cell跳转 - (void)onselectedtablerow:(rcconversationmodeltype)conversationmodeltype conversationmodel:(rcconversationmodel *)model atindexpath:(nsindexpath *)indexpath{ if (indexpath.row == 0) { wtforumandconnectlistviewcontroller *chatlist = (wtforumandconnectlistviewcontroller *)[wtstoryboardsegment instantiateviewcontrollerwithstoryboardname:@"main" identifier:@"wtforumandconnectlistviewcontroller"]; chatlist.title = @"回复和@我的"; [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 { //点击cell,拿到cell对应的model,然后从model中拿到对应的rcuserinfo,然后赋值会话属性,进入会话 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 在任意地方获取聊天列表数量及删除列表
获取聊天列表
nsarray *privatearr = [[rcimclient sharedrcimclient] getconversationlist:@[@(conversationtype_private)]];
在conversationlist添加对应类型的聊天就可以获取对应类型的聊天列表删除方法类似
[[rcimclient sharedrcimclient] clearconversations:@[@(conversationtype_private)]];
6 背景图
融云聊天列表没有数据的默认图片下面有点击右上角加入聊天,可是不是所有的聊天都有这个功能(我的就没有)如何没有就可以在资源文件中找到 no_message_img 这张图片用ps去掉下面的那一行字????
7 其它
以上就是我在使用融云过程中遇到的一些问题及解决方法,如果有错误或者不足之处还望指正,谢谢!也希望大家多多支持。