iOS整合融雲SDK即時通訊整理

weixin_33912246發表於2018-03-29

最近很少寫一下專案總結了,最近專案雖然做了很多,但是都是一些外包專案,做下來也沒有什麼值得總結的。最近一個專案用到了融雲即時通訊,以前基本都是用環信,所以還遇到了一些問題,在此總結一下記錄一下。

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 其它

以上就是我在使用融雲過程中遇到的一些問題及解決方法,如果有錯誤或者不足之處還望指正,謝謝!

相關文章