融云即时通讯的集成(一)

2018-11-08  本文已影响0人  这个姑凉儿

项目中一直用到融云的即时通讯,终于忙过那阵,总结一下😊

  1. 前期的准备
    • 注册开发者账号
    • 下载sdk
    • 创建应用
  2. sdk的集成
target 'MyApp' do
   pod 'RongCloudIM/IMLib', '~> 2.8.3'
   pod 'RongCloudIM/IMKit', '~> 2.8.3'  
end

pod instal

完成后,CocoaPods 会在您的工程根目录下生成一个.xcworkspace 文件。您需要通过此文件打开您的工程,而不是之前的 .xcodeproj。打开工程编译一下,如果没有报错恭喜你集成成功了。

  1. 代码集成
#import <RongIMKit/RongIMKit.h>
[[RCIM sharedRCIM] initWithAppKey:@"YourTestAppKey"];
[[RCIM sharedRCIM] connectWithToken:@"YourTestUserToken"     success:^(NSString *userId) {
   NSLog(@"登陆成功。当前登录的用户ID:%@", userId);
} error:^(RCConnectErrorCode status) {
   NSLog(@"登陆的错误码为:%d", status);
} tokenIncorrect:^{
   //token过期或者不正确。
   //如果设置了token有效期并且token过期,请重新请求您的服务器获取新的token
   //如果没有设置token有效期却提示token错误,请检查您客户端和服务器的appkey是否匹配,还有检查您获取token的流程。
   NSLog(@"token错误");
}];
//可以让自己的后台返回名字头像id,这些,然后传给融云
RCUserInfo *user = [[RCUserInfo alloc]init];
user.userId = USERID; 
user.name = RongYunName;
user.portraitUri = PortraitUri;
[RCIM sharedRCIM].currentUserInfo = user;
[RCIM sharedRCIM].userInfoDataSource = self;
[RCIM sharedRCIM].receiveMessageDelegate = self;
[RCIM sharedRCIM].connectionStatusDelegate = self;
#import <RongIMKit/RongIMKit.h>

@interface ChatMainController : RCConversationListViewController

@end
//在.m里重写下面的方法
//重写RCConversationListViewController的onSelectedTableRow事件

/**
 *  点击进入会话页面
 *
 *  @param conversationModelType 会话类型
 *  @param model                 会话数据
 *  @param indexPath             indexPath description
 */
- (void)onSelectedTableRow:(RCConversationModelType)conversationModelType
         conversationModel:(RCConversationModel *)model
               atIndexPath:(NSIndexPath *)indexPath {
    
    if (conversationModelType == RC_CONVERSATION_MODEL_TYPE_NORMAL) {
        //新建一个聊天会话View Controller对象,建议这样初始化
        ZJChatViewController *chat = [[ZJChatViewController alloc] initWithConversationType:ConversationType_PRIVATE targetId:model.targetId];
        //设置聊天会话界面要显示的标题
        chat.title = model.conversationTitle;
        //显示聊天会话界面
        [self.navigationController pushViewController:chat animated:YES];
    }

}
#import <RongIMKit/RongIMKit.h>

@interface ZJChatViewController : RCConversationViewController


@end

发送消息的方法

- (RCMessageContent *)willSendMessage:(RCMessageContent *)messageContent {
    RCUserInfo * senderUserInfo = [[RCUserInfo alloc] init];
    senderUserInfo.userId = USERID;
    senderUserInfo.name = RongYunName;
    senderUserInfo.portraitUri = PortraitUri;
    messageContent.senderUserInfo = senderUserInfo;
    return messageContent;
}

接收消息的方法我放在了AppDelegate

- (void)onRCIMReceiveMessage:(RCMessage *)message left:(int)left {
    RCMessageContent * messageContent = message.content;
    [[RCIM sharedRCIM] refreshUserInfoCache:messageContent.senderUserInfo withUserId:message.senderUserId];
    RCUserInfo * senderUserInfo = messageContent.senderUserInfo;
    if (senderUserInfo != nil) {
        NSDictionary * dic = @{@"userId" : senderUserInfo.userId,
                               @"name" : senderUserInfo.name,
                               @"portraitUri" : senderUserInfo.portraitUri
                               };
//存储信息
        if (![[ZJRCUserInfoManager sharedManager] isBookExistsForKey:message.senderUserId]) {
            [[ZJRCUserInfoManager sharedManager] writePlist:dic forKey:message.senderUserId];
        } else {
            [[ZJRCUserInfoManager sharedManager] replaceDictionary:dic withDictionaryKey:message.senderUserId];
        }
    }
    
}

融云链接状态的监听

- (void)onRCIMConnectionStatusChanged:(RCConnectionStatus)status {
    switch (status) {
        case ConnectionStatus_KICKED_OFFLINE_BY_OTHER_CLIENT: {           // 被踢下线
            [SVProgressHUD showInfoWithStatus:@"此账号已在其他设备登录,请重新登录"];
            
            break;
        }
        default:
            break;
    }
}

到此,就完成了融云的基本聊天。
下篇进行融云的扩展功能:自定义消息,客服的功能

(如有不足,请及时指出,虚心接受😊)

上一篇 下一篇

猜你喜欢

热点阅读