您的位置:首页 > 移动开发 > IOS开发

iOS - 融云即时通讯的简单使用

2016-03-29 16:23 846 查看
项目里开始需要聊天功能,以前没有接触过,最近看着官方集成文档不是很操心。

集成官方文档地址

集成步骤就不说了 文档说的很清楚啦~

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: