集成融云即时通讯第一弹

1:首先在融云官网创建应用,然后进行API调试,首先要获取token





主要是拿到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



这是AppDelegate.m文件

#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


这是MyTestChatViewController.m

#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];

}

//重写RCConversationListViewControlleronSelectedTableRow事件

- (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



这是ChatController.m

#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:好了,到这里代码已经全部完毕,下面要详细讲解一下

首先在viewController.m里面创建两个按钮

- (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];


   

}

运行程序:如图
然后拿到获取到的token在appdelegate.m里面连接融云的服务器:

这句话很重要在

- (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中

//重写RCConversationListViewControlleronSelectedTableRow事件

- (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调试:



然后点击进入讨论组就会看到,如图:


这样就简单的集成了融云的聊天界面,可以到github上面下载源码 https://github.com/dengdy/RCOA
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值