融云即时通讯(一)

第一次写文章,用来记录自己研究的融云iOS开发。

http://www.rongcloud.cn/docs/ios.html 这个是官方的文档,看了非常有帮助,有不懂的也可以查询。

(1) 使用cocoa pods导入,使用[[RCIMsharedRCIM]initWithAppKey:AppKey]类方法,连接融云服务器。

(2)[[RCIMsharedRCIM]connectWithToken:APPTokensuccess:^(NSString *userId) {

        NSLog(@"登陆成功。当前登录的用户ID%@", userId);

    } error:^(RCConnectErrorCode status) {

        NSLog(@"登陆的错误码为:%ld", (long)status);

    } tokenIncorrect:^{

        //token过期或者不正确。

        //如果设置了token有效期并且token过期,请重新请求您的服务器获取新的token

        //如果没有设置token有效期却提示token错误,请检查您客户端和服务器的appkey是否匹配,还有检查您获取token的流程。

        NSLog(@"token错误");

    }];

使用上面的方法通过token值登陆,这里要说的是token值要通过请求获取,融云建议的是自己公司的服务器端向融云服务器请求,然后再由app端向自己公司的服务器请求,这是为了安全考虑。在自己测试时可以自己写个请求方法,要注意设置请求头,具体的可以自行百度,也可以使用融云的api调试工具获取token。(提示一下,如果很久以前申请的app应用信息可能会出现请求失败,我就被坑了很久,然后重新申请了一个应用)。

(3)一对一的聊天窗口

    RCConversationViewController *chat = [[RCConversationViewControlleralloc]init];

    //设置会话的类型,如单聊、讨论组、群聊、聊天室、客服、公众服务会话等

    chat.conversationType =ConversationType_PRIVATE;

    //设置会话的目标会话ID。(单聊、客服、公众服务会话为对方的ID,讨论组、群聊、聊天室为会话的ID

    chat.targetId =@"对方的id";

    //设置聊天会话界面要显示的标题

    chat.title =@"想显示的会话标题";

    //显示聊天会话界面

    [self.navigationControllerpushViewController:chatanimated:YES];

使用上面的方法就能进入集成好的聊天页面了

(4)用户的头像和昵称显示

这里融云是没有方法直接获取头像和昵称的数据的,所以我们需要通过自己公司服务器获取和存储数据,然后设置数据。

<RCIMUserInfoDataSource>实现代理

[[RCIMsharedRCIM]setUserInfoDataSource:self];

需要重写这个代理方法

- (void)getUserInfoWithUserId:(NSString *)userId completion:(void (^)(RCUserInfo *))completion{

        RCUserInfo *userinfo = [[RCUserInfoalloc]init];

        userinfo.userId = userId;

//通过网络请求获取name和URL

        userinfo.name =@“name”;

        NSString *image =@"http://tx.haiqq.com/uploads/allimg/160417/1_041F15KK0L.jpg"; //头像的url

        completion(userinfo);

}

当进入到聊天界面的时候系统会自动调用这个代理方法设置,注意测试的时候使用图片不能太大,不然显示不出来,不要超过200*200,最好是100*100以内

这时候就会显示出头像和昵称了。融云还提供了刷新用户信息的方法,不过只是在推送时用到。

- (void)refreshUserInfoCache:(RCUserInfo *)userInfo withUserId:(NSString *)userId;//通过单例调用

设置当前的用户信息

[RCIMsharedRCIM].currentUserInfo = userinfo;

[RCIMsharedRCIM].globalConversationAvatarStyle =RC_USER_AVATAR_CYCLE;//设置头像是圆形还是方形,默认是方形,聊天列表

[RCIMsharedRCIM].globalConversationPortraitSize =CGSizeMake(50,50);//设置头像大小,默认是46*46,设置时必须大于36*36,否则无效

    [RCIMsharedRCIM].globalMessageAvatarStyle =RC_USER_AVATAR_CYCLE;//聊天对话窗口

[RCIMsharedRCIM].globalMessagePortraitSize = CGSizeMake(50,50);


(5)聊天列表的显示

这里是自己新建一个类 YourTestChatViewController ,继承自RCConversationListViewController

- (void)viewDidLoad {

    //重写显示相关的接口,必须先调用super,否则会屏蔽SDK默认的处理

    [superviewDidLoad];

    

    //设置需要显示哪些类型的会话

    [selfsetDisplayConversationTypes:@[@(ConversationType_PRIVATE),

                                        @(ConversationType_DISCUSSION),

                                        @(ConversationType_CHATROOM),

                                        @(ConversationType_GROUP),

                                        @(ConversationType_APPSERVICE),

                                        @(ConversationType_SYSTEM)]];

    //设置需要将哪些类型的会话在会话列表中聚合显示

    [selfsetCollectionConversationType:@[@(ConversationType_DISCUSSION),

                                          @(ConversationType_GROUP)]];

}


- (void)onSelectedTableRow:(RCConversationModelType)conversationModelType

         conversationModel:(RCConversationModel *)model

               atIndexPath:(NSIndexPath *)indexPath {

    RCConversationViewController *conversationVC = [[RCConversationViewControlleralloc]init];

    conversationVC.conversationType = model.conversationType;

    conversationVC.targetId = model.targetId;

    conversationVC.title =@"想要的title";

    [self.navigationControllerpushViewController:conversationVCanimated:YES];

}

这两个方法是直接融云文档上抄的,还没研究过,不过显示的是一个对话的列表页面。

(6)讨论组和聊天室创建

讨论组

 NSArray *array = [NSArrayarrayWithObjects:@"111",@"123",@"0001",@"0002",nil];

    [[RCIMsharedRCIM] createDiscussion:@"讨论组"userIdList:array success:^(RCDiscussion *discussion) {

        dispatch_sync(dispatch_get_main_queue(), ^{//这个必须是在主线程执行,因为融云的讨论组创建方法是在子线程创建的,所以不调用主线程就不能跳转

            [selfset:discussion];

        });        

    } error:^(RCErrorCode status) {

        NSLog(@"%ld",(long)status);

    }];


- (void)set:(RCDiscussion *)discussion{

    RCConversationViewController *chatRoom = [[RCConversationViewControlleralloc]init];

    chatRoom.conversationType =ConversationType_DISCUSSION;

    chatRoom.targetId = discussion.discussionId;

    chatRoom.title = discussion.discussionName;

    //设置加入聊天室时需要获取的历史消息数量,最大值为50

    chatRoom.defaultHistoryMessageCountOfChatRoom =20;

    [self.navigationControllerpushViewController:chatRoomanimated:YES];

}

聊天室

RCConversationViewController *chat = [[RCConversationViewController alloc]init];

        //设置会话的类型为聊天室

        chat.conversationType = ConversationType_CHATROOM;

        //设置会话的目标会话ID

        chat.targetId = @"123456";

        //设置聊天会话界面要显示的标题

        chat.title = @"聊天室";

        //设置加入聊天室时需要获取的历史消息数量,最大值为50

        chat.defaultHistoryMessageCountOfChatRoom = 20;

        //显示聊天会话界面

        [self.navigationController pushViewController:chat animated:YES];


(7)获取对话列表

NSArray *array = [NSArrayarrayWithObjects:[NSNumbernumberWithInteger:ConversationType_PRIVATE],[NSNumbernumberWithInteger:ConversationType_DISCUSSION],nil];

    NSArray *listArray = [[RCIMClient sharedRCIMClient]getConversationList:array];

(8)扩展功能板块的自定义

首先要新建一个类,继承自RCConversationViewController

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    [self.pluginBoardView insertItemWithImage:imagePic

                                        title:title

                                      atIndex:0

                                          tag:101];

//    或添加到最后的:

    [self.pluginBoardView insertItemWithImage:imagePic

                                        title:title

                                          tag:101];

//    删除指定位置的方法:

    [self.pluginBoardViewremoveItemAtIndex:0];

//    删除指定标签的方法:

    [self.pluginBoardViewremoveItemWithTag:101];

//    删除所有:

    [self.pluginBoardViewremoveAllItems];

//    更换现有扩展项的图标和标题:

    [self.pluginBoardView updateItemAtIndex:0 image:newImage title:newTitle];

//    或者根据tag来更换

    [self.pluginBoardView updateItemWithTag:101 image:newImage title:newTitle];

}

//重写点击方法

- (void)pluginBoardView:(RCPluginBoardView *)pluginBoardView clickedItemWithTag:(NSInteger)tag{

//系统本来带有3个扩展功能,tag=1001是照片,tag=1002是拍照,tag=1003是定位

    [superpluginBoardView:pluginBoardViewclickedItemWithTag:tag];

    if (tag ==101) {

        //点击事件处理

    }

}

(10)改变聊天背景

在 RCConversationViewController 的继承类的 viewDidLoad 方法中,加入以下代码即可设置聊天界面背景图:

self.conversationMessageCollectionView.backgroundView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"图片名"]];

但是上面的方法在frame变换时会变形,下面可以解决

//设置conversationMessageCollectionView的背景色为透明

    self.conversationMessageCollectionView.backgroundColor = [UIColorclearColor];

    //设置self.view.backgroundColor用自己的背景图片

    self.view.backgroundColor = [UIColorcolorWithPatternImage:[UIImageimageNamed:@""]];

(11)发送消息

- (void)sendMessage:(RCMessageContent *)messageContent pushContent:(NSString *)pushContent; //文字

- (void)sendImageMessage:(RCImageMessage *)imageMessage pushContent:(NSString *)pushContent; //图片

- (void)resendMessage:(RCMessageContent *)messageContent;//重新发送

- (void)deleteMessage:(RCMessageModel *)model;//删除消息


总结一下,使用IMKit框架的话,融云有继承好的RCConversationViewController类,直接调用就可以用,也可以自己写一个类,继承自RCConversationViewController,在用到的方法里面使用[super 。。。]就可以使用融云添加好的方法,后面可以自己添加想要的内容,在聊天界面需要使用的接口操作可以查看RCConversationViewController.h,里面方法介绍很详细也很全面。如果还需要功能只能自己写或者在融云上提交工单,申请开发。




  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值