1:首先在融云官网创建应用,然后进行API调试,首先要获取token
2:啥也不多说,直接上干货
这是ViewController.h文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
这是ViewController.m文件
#import "ViewController.h"
#import "MyTestChatViewController.h"
#import <RongIMKit/RongIMKit.h>
#import "ChatController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColorwhiteColor];
UIButton *btn1 = [UIButtonbuttonWithType:UIButtonTypeCustom];
btn1.frame =CGRectMake(50,200,200,30);
[btn1 setTitle:@"聚合单聊展示"forState:UIControlStateNormal];
[btn1 setTitleColor:[UIColorblackColor]forState:UIControlStateNormal];
[btn1 addTarget:selfaction:@selector(private:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btn1];
UIButton *btn2 = [UIButtonbuttonWithType:UIButtonTypeCustom];
btn2.frame =CGRectMake(50,240,200,30);
[btn2 setTitle:@"讨论组"forState:UIControlStateNormal];
[btn2 setTitleColor:[UIColorblackColor]forState:UIControlStateNormal]; [btn2addTarget:selfaction:@selector(discuss:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btn2];
}
-(void)private:(UIButton *)sender{
MyTestChatViewController *chatVc = [[MyTestChatViewControlleralloc]init];
//设置需要将哪些类型的会话在会话列表中聚合显示
[chatVc setCollectionConversationType:@[@(ConversationType_PRIVATE)]];
[self.navigationControllerpushViewController:chatVcanimated:YES];
}
-(void)discuss:(UIButton *)sender{
NSArray *array = [NSArrayarrayWithObjects:@"1",@"2",@"3",nil];
__weakViewController *weakSelf =self;
[[RCIMClientsharedRCIMClient]createDiscussion:@"3个火枪手"userIdList:arraysuccess:^(RCDiscussion *discussion) {
dispatch_async(dispatch_get_main_queue(), ^{
ChatController *chat =
[[ChatControlleralloc]init];
chat.targetId = discussion.discussionId;
chat.conversationType =ConversationType_DISCUSSION;
chat.title = discussion.discussionName;
[weakSelf.navigationControllerpushViewController:chatanimated:YES];
});
} error:^(RCErrorCode status) {
}];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
这是AppDelegate.h文件
#import <UIKit/UIKit.h>
#import <RongIMKit/RongIMKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong,nonatomic)UIWindow *window;
@end
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()<RCIMUserInfoDataSource>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds];
ViewController *myTestChatVc = [[ViewControlleralloc]init];
UINavigationController *nav = [[UINavigationControlleralloc]initWithRootViewController:myTestChatVc];
self.window.rootViewController = nav;
[self.windowmakeKeyAndVisible];
[[RCIMsharedRCIM]initWithAppKey:@"0vnjpoadn2viz"];
[selfconnectServer];
[[RCIMsharedRCIM]setUserInfoDataSource:self];
[RCIMsharedRCIM].currentUserInfo = [[RCUserInfoalloc]initWithUserId:@"1"name:@"章鱼哥"portrait:@"http://downhdlogo.yy.com/hdlogo/640640/640/640/69/0387691937/u3876919371md9Q4MB.jpg?20160704144001"];
returnYES;
}
-(void)connectServer{
[[RCIMsharedRCIM]connectWithToken:@"ep6VJw5kvJ9kEgxa8nrpGQ5tTbbgcx39Iq4bQefrF7XUGR44vn86liCT5bwOm6Un/4X3IufCqXvlP+8/lVo5xA==" success:^(NSString *userId) {
NSLog(@"登陆成功。当前登录的用户ID:%@", userId);
} error:^(RCConnectErrorCode status) {
NSLog(@"登陆的错误码为:%ld", status);
} tokenIncorrect:^{
//token过期或者不正确。
//如果设置了token有效期并且token过期,请重新请求您的服务器获取新的token
//如果没有设置token有效期却提示token错误,请检查您客户端和服务器的appkey是否匹配,还有检查您获取token的流程。
NSLog(@"token错误");
}];
}
-(void)getUserInfoWithUserId:(NSString *)userId completion:(void (^)(RCUserInfo *))completion{
if ([userIdisEqualToString:@"2"]){
RCUserInfo *userInfo = [[RCUserInfoalloc]initWithUserId:@"2"name:@"派大星"portrait:@"http://www.asiafinance.cn/u/cms/www/201512/min22142854s8f9.jpg"];
completion(userInfo);
}elseif ([userIdisEqualToString:@"3"]){
RCUserInfo *userInfo = [[RCUserInfoalloc]initWithUserId:@"3"name:@"海绵宝宝"portrait:@"http://i.dimg.cc/0a/fe/28/8b/2d/78/b5/b0/5e/15/32/35/2e/5d/1e/ec.jpg"];
completion(userInfo);
}
}
这是MyTestChatViewController.h自定义列表类
#import <RongIMKit/RongIMKit.h>
@interface MyTestChatViewController :RCConversationListViewController
@end
#import"MyTestChatViewController.h"
#import"ChatController.h"
@interfaceMyTestChatViewController ()
@end
@implementation MyTestChatViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
//设置需要显示哪些类型的会话
[selfsetDisplayConversationTypes:@[@(ConversationType_PRIVATE)]];
// self.cellBackgroundColor = [UIColor orangeColor];
//设置cell的背景颜色
// self.cellBackgroundColor = [UIColor cyanColor];
//设置置顶的cell的背景颜色(这个目前测试无效)
self.topCellBackgroundColor = [UIColoryellowColor];
}
//重写RCConversationListViewController的onSelectedTableRow事件
- (void)onSelectedTableRow:(RCConversationModelType)conversationModelType
conversationModel:(RCConversationModel *)model
atIndexPath:(NSIndexPath *)indexPath {
if (conversationModelType ==RC_CONVERSATION_MODEL_TYPE_COLLECTION) {
MyTestChatViewController *textVC = [[MyTestChatViewControlleralloc]init];
textVC.isEnteredToCollectionViewController =YES;
NSArray *array = [NSArrayarrayWithObject:[NSNumbernumberWithInt:model.conversationModelType]];
[textVCsetDisplayConversationTypeArray:array];
[textVC setCollectionConversationType:nil];
[self.navigationController pushViewController:textVC animated:YES];
}elseif (conversationModelType == RC_CONVERSATION_MODEL_TYPE_NORMAL){
ChatController *conversationVC = [[ChatController alloc]init];
conversationVC.conversationType = model.conversationType;
//设置是否显示昵称
conversationVC.displayUserNameInCell =NO;
conversationVC.targetId = model.targetId;
conversationVC.title = model.conversationTitle;
[self.navigationController pushViewController:conversationVC animated:YES];
}
}
- (void)willDisplayConversationTableCell:(RCConversationBaseCell *)cell
atIndexPath:(NSIndexPath *)indexPath{
RCConversationCell *cell1 = (RCConversationCell *)cell;
cell1.conversationTitle.textColor = [UIColor redColor];
cell1.portraitStyle = RC_USER_AVATAR_CYCLE;
RCConversationModel *model=
self.conversationListDataSource[indexPath.row];
if (model.conversationType == ConversationType_PRIVATE) {
((RCConversationCell *)cell).isShowNotificationNumber =YES;
}
}
@end
这是自定义聊天界面ChatController.h继承于RCConversationViewController
#import <RongIMKit/RongIMKit.h>
@interface ChatController :RCConversationViewController
@end
#import "ChatController.h"
@interface ChatController ()
@end
@implementation ChatController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
//设置背景颜色
self.conversationMessageCollectionView.backgroundColor =[UIColoryellowColor];
//设置背景图片
self.conversationMessageCollectionView.backgroundColor = [UIColorcolorWithPatternImage:[UIImageimageNamed:@"af882ac3f3e2de63230f8b9f4b928e59.jpeg"]];
}
@end
3:好了,到这里代码已经全部完毕,下面要详细讲解一下
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColorwhiteColor];
UIButton *btn1 = [UIButtonbuttonWithType:UIButtonTypeCustom];
btn1.frame =CGRectMake(50,200,200,30);
[btn1 setTitle:@"聚合单聊展示"forState:UIControlStateNormal];
[btn1 setTitleColor:[UIColorblackColor]forState:UIControlStateNormal];
[btn1 addTarget:selfaction:@selector(private:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btn1];
UIButton *btn2 = [UIButtonbuttonWithType:UIButtonTypeCustom];
btn2.frame =CGRectMake(50,240,200,30);
[btn2 setTitle:@"讨论组"forState:UIControlStateNormal];
[btn2 setTitleColor:[UIColorblackColor]forState:UIControlStateNormal]; [btn2addTarget:selfaction:@selector(discuss:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btn2];
}
运行程序:如图- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[[ RCIM sharedRCIM ] initWithAppKey : @"0vnjpoadn2viz" ];-(void)connectServer{
[[RCIMsharedRCIM]connectWithToken:@"ep6VJw5kvJ9kEgxa8nrpGQ5tTbbgcx39Iq4bQefrF7XUGR44vn86liCT5bwOm6Un/4X3IufCqXvlP+8/lVo5xA==" success:^(NSString *userId) {
NSLog(@"登陆成功。当前登录的用户ID:%@", userId);
} error:^(RCConnectErrorCode status) {
NSLog(@"登陆的错误码为:%ld", status);
} tokenIncorrect:^{
//token过期或者不正确。
//如果设置了token有效期并且token过期,请重新请求您的服务器获取新的token
//如果没有设置token有效期却提示token错误,请检查您客户端和服务器的appkey是否匹配,还有检查您获取token的流程。
NSLog(@"token错误");
}];
}
[[RCIMsharedRCIM]setUserInfoDataSource:self];
[RCIMsharedRCIM].currentUserInfo = [[RCUserInfoalloc]initWithUserId:@"1"name:@"章鱼哥"portrait:@"http://downhdlogo.yy.com/hdlogo/640640/640/640/69/0387691937/u3876919371md9Q4MB.jpg?20160704144001"];
@interface AppDelegate ()<RCIMUserInfoDataSource>
设置代理对象 [[RCIMsharedRCIM]setUserInfoDataSource:self];
然后调用代理方法
-(void)getUserInfoWithUserId:(NSString *)userId completion:(void (^)(RCUserInfo *))completion{
if ([userIdisEqualToString:@"2"]){
RCUserInfo *userInfo = [[RCUserInfoalloc]initWithUserId:@"2"name:@"派大星"portrait:@"http://www.asiafinance.cn/u/cms/www/201512/min22142854s8f9.jpg"];
completion(userInfo);
}elseif ([userIdisEqualToString:@"3"]){
RCUserInfo *userInfo = [[RCUserInfoalloc]initWithUserId:@"3"name:@"海绵宝宝"portrait:@"http://i.dimg.cc/0a/fe/28/8b/2d/78/b5/b0/5e/15/32/35/2e/5d/1e/ec.jpg"];
completion(userInfo);
}
}
这样你就有两个人跟你聊天了
在MyTestChatViewController.m中
//重写RCConversationListViewController的onSelectedTableRow事件
- (void)onSelectedTableRow:(RCConversationModelType)conversationModelType
conversationModel:(RCConversationModel *)model
atIndexPath:(NSIndexPath *)indexPath {
if (conversationModelType ==RC_CONVERSATION_MODEL_TYPE_COLLECTION) {
MyTestChatViewController *textVC = [[MyTestChatViewControlleralloc]init];
textVC.isEnteredToCollectionViewController =YES;
NSArray *array = [NSArrayarrayWithObject:[NSNumbernumberWithInt:model.conversationModelType]];
[textVC setDisplayConversationTypeArray:array];
[textVC setCollectionConversationType:nil];
[self.navigationControllerpushViewController:textVCanimated:YES];
}elseif (conversationModelType ==RC_CONVERSATION_MODEL_TYPE_NORMAL){
ChatController *conversationVC = [[ChatControlleralloc]init];
conversationVC.conversationType = model.conversationType;
//设置是否显示昵称
conversationVC.displayUserNameInCell =YES;
conversationVC.targetId = model.targetId;
conversationVC.title = model.conversationTitle;
[self.navigationControllerpushViewController:conversationVCanimated:YES];
}
}
//设置cell列表的一些字体颜色,未读数字等等
- (void)willDisplayConversationTableCell:(RCConversationBaseCell *)cell
atIndexPath:(NSIndexPath *)indexPath{
RCConversationCell *cell1 = (RCConversationCell *)cell;
cell1.conversationTitle.textColor = [UIColorredColor];
cell1.portraitStyle =RC_USER_AVATAR_CYCLE;
RCConversationModel *model=
self.conversationListDataSource[indexPath.row];
if (model.conversationType ==ConversationType_PRIVATE) {
//会话中有未读消息时,是否在头像右上角的bubbleTipView中显示数字
((RCConversationCell *)cell).isShowNotificationNumber =YES;
}
}
-(void)discuss:(UIButton *)sender{
NSArray *array = [NSArrayarrayWithObjects:@"1",@"2",@"3",nil];
__weakViewController *weakSelf =self;
[[RCIMClientsharedRCIMClient]createDiscussion:@"3个火枪手"userIdList:arraysuccess:^(RCDiscussion *discussion) {
dispatch_async(dispatch_get_main_queue(), ^{
ChatController *chat =
[[ChatControlleralloc]init];
chat.targetId = discussion.discussionId;
chat.conversationType =ConversationType_DISCUSSION;
chat.title = discussion.discussionName;
[weakSelf.navigationControllerpushViewController:chatanimated:YES];
});
} error:^(RCErrorCode status) {
}];
}
然后进行API调试: