iOS 腾讯云通信-腾讯IM接入弹幕功能

来源:互联网 发布:中国女性受侵害数据 编辑:程序博客网 时间:2024/05/21 11:06


       项目中涉及到弹幕功能,所以研究了一下平常直播中弹幕功能的实现,弹幕的UI是一个现成的UI类,不喜欢可自行编写,写好的架子放在底下链接中,先说一下UI的使用:


  1.首先在你将要使用的类中引用到这几个

    //弹幕view

       #import"BulletView.h"

    #import"BulletManager.h"

    #import "BulletBackgroudView.h"


引用到这几个类后可直接调用里面的方法使用

其中

显示弹幕的方法

  [self.bulletManager start:elemArr];

     关闭弹幕的方法
   

  [self.bulletManager stop

    

     UI的原理很简单,这里直接将数据传过去就可以了,其他自己看下里面的内容就好了,这里不过多描述;


    2.腾讯IM接入工作:


先根据文档中的配置接入IM的SDK,文档地址:https://www.qcloud.com/document/product/269/9147?!preview=true&lang=zh

      

        然后是项目中的使用:

        首先需要初始化SDK 以及 登录



   //初始化及登录

  -(void)initIMSDK

  {

   NSDictionary * userinfo=[[NSUserDefaultsstandardUserDefaults]objectForKey:@"user"];

   //获取用户签名

   NSDictionary * param = [NSDictionarydictionaryWithObjectsAndKeys:userinfo[@"registerid"],@"identifier",nil];

   WYLog(@"%@",param);

    [WYHttpToolgetHttps:Get_GenSigparam:paramSuccess:^(NSDictionary *dict,BOOL success) {

        WYLog(@"%@",dict);

        if ([[dictobjectForKey:@"code"]intValue]==0)

        {

            //初始化sdk

           TIMManager *manager = [TIMManagersharedInstance];

            TIMSdkConfig *config = [[TIMSdkConfigalloc]init];

            config.sdkAppId =1400031822;

            config.accountType =@"13058";

            config.disableCrashReport =NO;

            config.connListener =self;

            [managerinitSdk:config];

            

           TIMUserConfig *userConfig = [[TIMUserConfigalloc]init];

            userConfig.disableRecnetContact =NO;//不开启最近联系人(加载消息扩展包有效)

            userConfig.enableFriendshipProxy =YES;//开启关系链数据本地缓存功能(加载好友扩展包有效)

            userConfig.enableGroupAssistant =YES;//开启群组数据本地缓存功能(加载群组扩展包有效)

           TIMGroupInfoOption *giOption = [[TIMGroupInfoOptionalloc]init];

           userConfig.userStatusListener =self;//用户登录状态监听器

            userConfig.refreshListener =self;//会话刷新监听器(未读计数、已读同步)(加载消息扩展包有效)

            userConfig.friendshipListener =self;//关系链数据本地缓存监听器(加载好友扩展包、enableFriendshipProxy有效)

            userConfig.groupListener =self;//群组据本地缓存监听器(加载群组扩展包、enableGroupAssistant有效)

            [manager setUserConfig:userConfig];

            //添加消息回调

            [manageraddMessageListener:self];

           elemArr = [[NSMutableArrayalloc]init];

            //用户登录

           // identifier为用户名,userSig为用户登录凭证

           // appidAt3rd在私有帐号情况下,填写与sdkAppId一样

           // identifier =用户id  userSig = 签名

           TIMLoginParam * login_param = [[TIMLoginParamalloc ]init];

            login_param.identifier = [NSStringstringWithFormat:@"%@",userinfo[@"registerid"]];

            login_param.userSig = [NSStringstringWithFormat:@"%@",[[dictobjectForKey:@"data"]objectForKey:@"userSig"]];

            login_param.appidAt3rd =@"1400031822";


            [[TIMManagersharedInstance]login: login_paramsucc:^()

             {

                NSLog(@"登录成功..");

                WYLog(@"当前登录用户为-------%@",[manager getLoginUser]);

                 

             }fail:^(int code,NSString * err) {

                NSLog(@"登录失败: %d->%@", code, err);

                 [selfShowComplitedHUDWith:err];

             }];

        }else

        {

            [selfShowComplitedHUDWith:@"无法发送用户弹幕,请检查是否登录"];

        }

    } fail:^(NSError *error) {

    }];

 }



   

    发送消息:这段代码创建会话及发送消息(分为单聊和群聊,此处是群聊模式,单聊群聊,文档中有介绍)


                  TIMConversation * grp_conversation = [[TIMManagersharedInstance]getConversation:TIM_GROUPreceiver:self.liveDetailModel.groupId];

            TIMTextElem * text_elem = [[TIMTextElemalloc]init];

            //发送的消息

            [text_elem setText:textField.text];

            TIMMessage * msg = [[TIMMessagealloc]init];

            [msg addElem:text_elem];

            [grp_conversation sendMessage:msgsucc:^()

             {

                 WYLog(@"消息发送成功");

                 //清空消息框

                 //发送成功,添加到数组

                 [elemArrremoveAllObjects];

                 [elemArraddObject:textField.text];

                 [self.bulletManagerstart:elemArr];

                 sendMessageFiled.text =@"";

                 //隐藏键盘

                 [sendMessageFiledresignFirstResponder];

                 

             }fail:^(int code,NSString * err) {

                 WYLog(@"消息发送失败:%d->%@", code, err);

             }];





接收消息的回调以及消息解析,此回调方法必须注册才可生效,重复注册无效

//消息回调

- (void)onNewMessage:(NSArray*) msgs

{

    NSLog(@"新消息回调: %@", msgs);

    for (int i=0; i< msgs.count; i++) {

        TIMMessage *lastMsg =msgs[i];

        [lastMsg getElem:i];

        TIMElem * elem =[lastMsggetElem:i];

        if ([elemisKindOfClass:[TIMTextElemclass]]) {

            TIMTextElem * text_elem = (TIMTextElem * )elem;

            WYLog(@"收到的消息===%@",text_elem.text);

            [elemArrremoveAllObjects];

            [elemArraddObject:text_elem.text];

            //判断弹幕开关是否开启

            if (_mySwitch.on ==NO) {

                WYLog(@"弹幕关闭无法查看");

            }else

            {

                [self.bulletManagerstart:elemArr];

            }

       }

    }

    WYLog(@"消息数组===%@",elemArr);

}



 UI下载链接  http://download.csdn.net/detail/u010945891/9858883

       这里只介绍这几种比较重要的方法,也是留给自己以后备用,有错误的地方多多指教,谢谢。

原创粉丝点击