詳解iOS集成融云SDK即時通訊整理
最近很少寫一下項目總結了,最近項目雖然做了很多,但是都是一些外包項目,做下來也沒有什么值得總結的。最近一個項目用到了融云即時通訊,以前基本都是用環(huán)信,所以還遇到了一些問題,在此總結一下記錄一下。
1 頭像、昵稱等用戶信息(融云對這個問題有兩種處理方式)
1.用戶信息提供者
實現(xiàn)步驟(以下代碼放在單例中,可以是AppDelegate,最好單獨寫一個單例)
首先遵守RCIMUserInfoDataSource這個協(xié)議
然后是要設置代理
[[RCIM sharedRCIM] setUserInfoDataSource:self];
最后實現(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 來取得對應的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.在擴展消息中攜帶用戶信息
設置發(fā)送消息時在消息體中攜帶用戶信息(從2.4.1 之后附加用戶信息之后cell默認會顯示附加的用戶信息的頭像,即用戶信息不會取用戶信息提供者里提供的用戶信息)
[RCIM sharedRCIM].enableMessageAttachUserInfo = YES;
你設置了enableMessageAttachUserInfo之后,可以取到
/** * 發(fā)送者信息 * **/ @property(nonatomic, strong) RCUserInfo *senderUserInfo;
當然我覺得還可以從后臺獲取好友關系后,我們在每次登陸后,開一個線程把好友關系請求下來存起來然后根據(jù)環(huán)信ID查找好友的昵稱和頭像
2 給輸入框添加提示語(這個我一直覺得環(huán)信應該給了方法修改,只是我一直沒有找到這個方法,所以只有自己去寫了)
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需要實現(xiàn)群成員數(shù)據(jù)源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的數(shù)據(jù)源
- (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 在任意地方獲取聊天列表數(shù)量及刪除列表
獲取聊天列表
NSArray *privateArr = [[RCIMClient sharedRCIMClient] getConversationList:@[@(ConversationType_PRIVATE)]];
在ConversationList添加對應類型的聊天就可以獲取對應類型的聊天列表刪除方法類似
[[RCIMClient sharedRCIMClient] clearConversations:@[@(ConversationType_PRIVATE)]];
6 背景圖
融云聊天列表沒有數(shù)據(jù)的默認圖片下面有點擊右上角加入聊天,可是不是所有的聊天都有這個功能(我的就沒有)如何沒有就可以在資源文件中找到 no_message_img 這張圖片用ps去掉下面的那一行字😆
7 其它
以上就是我在使用融云過程中遇到的一些問題及解決方法,如果有錯誤或者不足之處還望指正,謝謝!也希望大家多多支持腳本之家。
相關文章
IOS開發(fā)代碼分享之用nstimer實現(xiàn)倒計時功能
在制作IOS項目中,我們經常要用到倒計時功能,今天就分享下使用nstimer實現(xiàn)的倒計時功能的代碼,希望對大家能有所幫助2014-09-09
實例解析iOS應用多線程開發(fā)中NSthread類的用法
這篇文章主要介紹了iOS應用多線程開發(fā)中NSthread類的用法,代碼基于傳統(tǒng)的Objective-C,NSthread類需要的朋友可以參考下2016-02-02

