iOS - 融云即时通讯的简单使用
2016-03-29 16:23
846 查看
项目里开始需要聊天功能,以前没有接触过,最近看着官方集成文档不是很操心。
集成官方文档地址
集成步骤就不说了 文档说的很清楚啦~
1⃣️
集成完毕,我们开始要写代码来做初始化和连接部分啦!
在appdelegate.m文件里 先包含文件
注:
token的获取我们可以先用融云平台里自己的API调试(在你们自己服务器未完成token部分,可以暂时先用)
返回的token就可以在上述代码中使用了!
2⃣️在ViewController.m里面写代码
先写一个按钮,点击跳转到聊天内容界面:
3⃣️ 做聊天列表的页面
在appdelegate.m中把根视图转为聊天列表页 而不是viewController
聊天列表的页面 继承RCConversationListViewController类
问题: 写到这里 我也有一点疑惑 聊天列表会显示跟你聊过天的所有的列表 并且删除了列表的联系人 下次运行的时候还有 是不是只有清空聊天记录缓存才不会显示聊天列表?
============ 有点事 以后更新==================
4⃣️ 获取用户信息 用来显示
在appdelegate.m中写
①遵守协议RCIMUserInfoDataSource
集成官方文档地址
集成步骤就不说了 文档说的很清楚啦~
1⃣️
集成完毕,我们开始要写代码来做初始化和连接部分啦!
在appdelegate.m文件里 先包含文件
#import <RongIMKit/RongIMKit.h>
//[RCIM sharedRCIM] 核心类 // 初始化融云SDK //appKey 从融云开发者平台创建应用后获取到的App Key [[RCIM sharedRCIM] initWithAppKey:@"y745wfm84ekbv"]; // 与融云服务器建立连接 //token 从您服务器端获取的token(用户身份令牌) [[RCIM sharedRCIM] connectWithToken:@"/c+PTsQd/mbXxYaLWFmL/Ml1/i98BQyyuh3ScYAwQgny/Z1fXR+A63sHXdPfa7mJFajsnYUO8xsEsR/vqCzApg==" success:^(NSString *userId) { NSLog(@"登陆成功。当前登录的用户ID:%@", userId); } error:^(RCConnectErrorCode status) { NSLog(@"登陆的错误码为:%d",(int)status); } tokenIncorrect:^{ NSLog(@"token错误"); }];
注:
token的获取我们可以先用融云平台里自己的API调试(在你们自己服务器未完成token部分,可以暂时先用)
返回的token就可以在上述代码中使用了!
2⃣️在ViewController.m里面写代码
先写一个按钮,点击跳转到聊天内容界面:
#import "ViewController.h" #import <RongIMKit/RongIMKit.h> @interface ViewController () - (IBAction)chat; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } - (IBAction)chat { //和userid为123456的用户建立聊天 //RCConversationViewController 聊天内容界面 只需要传几个字段便可以 IMKit已经封装好了页面的组件 RCConversationViewController *chatVc = [[RCConversationViewController alloc] init]; chatVc.title = @"毛毛"; chatVc.conversationType = ConversationType_PRIVATE; chatVc.targetId = @"123456"; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:chatVc]; [self presentViewController:nav animated:YES completion:nil]; } @end
3⃣️ 做聊天列表的页面
在appdelegate.m中把根视图转为聊天列表页 而不是viewController
//FriendsController是聊天列表页 FriendsController *friendsVc = [[FriendsController alloc] init]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:friendsVc]; self.window.rootViewController = nav;
聊天列表的页面 继承RCConversationListViewController类
#import "FriendsController.h" @interface FriendsController () @end @implementation FriendsController - (void)viewDidLoad { [super viewDidLoad]; //是否显示网络状态 self.isShowNetworkIndicatorView = YES; //是否显示连接状态 self.showConnectingStatusOnNavigatorBar = YES; //设置默认的聊天列表类型(是一个数组 数组中放着强转NSNumber类型的会话类型) //例如:ConversationType_PRIVATE 单聊 [self setDisplayConversationTypes:@[@(ConversationType_PRIVATE), @(ConversationType_DISCUSSION),@(ConversationType_GROUP),@(ConversationType_SYSTEM),@(ConversationType_APPSERVICE)]]; // 把很多群组放在一个列表的一行cell里 把很多的讨论组放在一个列表的一行cell里 [self setCollectionConversationType:@[@(ConversationType_GROUP),@(ConversationType_DISCUSSION)]]; //设置cell的背景颜色 self.cellBackgroundColor = [UIColor cyanColor]; //设置置顶的cell的背景颜色 self.topCellBackgroundColor = [UIColor yellowColor]; } #pragma mark - 点击会话列表中中的cell的回调 - (void)onSelectedTableRow:(RCConversationModelType)conversationModelType conversationModel:(RCConversationModel *)model atIndexPath:(NSIndexPath *)indexPath { //model 等于 self.conversationListDataSource[indexPath.row]; RCConversationViewController *vc = [[RCConversationViewController alloc] init]; vc.conversationType = model.conversationType; vc.targetId = model.targetId; vc.title = model.conversationTitle; [self.navigationController pushViewController:vc animated:YES]; }
问题: 写到这里 我也有一点疑惑 聊天列表会显示跟你聊过天的所有的列表 并且删除了列表的联系人 下次运行的时候还有 是不是只有清空聊天记录缓存才不会显示聊天列表?
============ 有点事 以后更新==================
4⃣️ 获取用户信息 用来显示
在appdelegate.m中写
①遵守协议RCIMUserInfoDataSource
相关文章推荐
- ios 关于如何消除在向cell中添加图片文字是时出现的重影问题
- IOS基础知识
- IOS基础知识
- iOS数据存储之CoreData封装MagicalRecord
- iOS 国际化
- iOS数据存储之CoreData(一)
- iOS自适应布局之Masonry(一)
- iOS post一个json字符串到服务器 评论
- iOS开发~FMDB的使用
- iOS学习笔记07--深拷贝与浅拷贝
- ios 关于如何调整cell.imageView中的ImageView大小及位置
- iOS 重写pop跳转的方法
- iOS应用程序生命周期(前后台切换,应用的各种状态)详解
- iOS-获取当前View所在的控制器
- iOS多线程编程技术之NSThread、Cocoa NSOperation、GCD
- iOS 中图片旋转的方法
- iOS完整学习路线图-对知识的回顾/整理
- 提示框第三方库之MBProgressHUD iOS toast效果 动态提示框效果
- iOS 自定义单元格分割线宽度和屏幕宽度不一致问题
- ios文件读取(二)