iOS客户端集成

首先开发者需要在环信管理后台注册,参考注册并创建应用

环信即时通讯SDK是环信直播聊天室产品的直接对接方案,在直播聊天室中直接体现在直播聊天室列表的获取,直播间内的消息收发、白名单、禁言、观众列表,包含自定义消息体的礼物消息、点赞消息、弹幕消息等功能。

环信即时通讯云iOS SDK 介绍及导入

使用CocoaPods导入SDK

1. CocoaPods安装。

如果已经安装了CocoaPods,直接进入下一步即可。

sudo gem install cocoapods

2. 使用CocoaPods导入环信SDK。

pod 'Hyphenate'

手动导入 SDK

1. 下载SDK:环信SDK

2. SDK中包含以下文件。

./ChatDemo-UI3.0
./ChatDemo-UI3.0.ipa
./EaseUI
./HyphenateFullSDK
./HyphenateSDK
./HyphenateVideoRecoder
./doc

3. 选择“./HyphenateSDK/Hyphenate.framework”,在开发者的项目中,向General > Embedded Binaries 中添加依赖库Hyphenate.framework。

初始化SDK

第 1 步:引入相关头文件。

#import <Hyphenate/Hyphenate.h>

第 2 步:在工程的 AppDelegate 中的以下方法中,调用 SDK 对应方法。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //AppKey:注册的AppKey,详细见下面注释。
    //apnsCertName:推送证书名(不需要加后缀),详细见下面注释。
    EMOptions *options = [EMOptions optionsWithAppkey:@"douser#istore"];
    options.apnsCertName = @"istore_dev";
    [[EMClient sharedClient] initializeSDKWithOptions:options];

    return YES;
}

// APP进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [[EMClient sharedClient] applicationDidEnterBackground:application];
}

// APP将要从后台返回
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [[EMClient sharedClient] applicationWillEnterForeground:application];
}

第 3 步:自动注册&登录。

NSString *uuidAccount = [UIDevice currentDevice].identifierForVendor.UUIDString;//默认账户id

uuidAccount = [[uuidAccount stringByReplacingOccurrencesOfString:@"-" withString:@""] lowercaseString]; //替换获得的uuid中的@“-”为@“”

NSString *defaultPwd = @"000000";//默认密码

//注册
[[EMClient sharedClient] registerWithUsername:uuidAccount password:defaultPwd completion:^(NSString *aUsername, EMError *aError) {
    //登录
    [[EMClient sharedClient] loginWithUsername:(NSString *)uuidAccount password:defaultPwd          completion:^(NSString *aUsername, EMError *aError) {
         //跳转到直播主页
    }
}

具体功能介绍,直播Demo源码。Demo中实现了创建直播、查看当前直播列表、直播、观看直播、消息发送、点赞等功能。

文件功能
EaseLiveTVListViewController直播聊天室列表页面
EaseLiveViewController观看直播页面
EasePublishViewController直播页面
EaseCreateLiveViewController直播聊天室信息自定义页面
EaseHttpManager直播相关接口
EaseDefaultDataHelper本地保存数据
EaseCustomMessageHelper自定义消息(赞/礼物/弹幕)帮助类

1. 加入聊天室。

//EaseHttpManager.h

/*
 *  用户加入直播聊天室
 *
 *  @param aRoomId          直播聊天室ID
 *  @param aChatroomId      聊天室ID
 *  @param aIsCount         是否计数
 *  @param aCompletion      完成的回调block
 */
- (void)joinLiveRoomWithRoomId:(NSString*)aRoomId
                    chatroomId:(NSString*)aChatroomId
                       isCount:(BOOL)aIsCount
                    completion:(void (^)(BOOL success))aCompletion
                    
 //示例代码
 [[EaseHttpManager sharedInstance] joinLiveRoomWithRoomId:@"直播室ID"
                                               chatroomId:@"聊天室ID"
                                                  isCount:YES
                                               completion:^(BOOL success) {
                                               }];

2. 离开聊天室。

//EaseHttpManager.h

/*
 *  用户离开直播聊天室
 *
 *  @param aRoomId          直播聊天室ID
 *  @param aChatroomId      聊天室ID
 *  @param aIsCount         是否计数
 *  @param aCompletion      完成的回调block
 */
- (void)leaveLiveRoomWithRoomId:(NSString*)aRoomId
                     chatroomId:(NSString*)aChatroomId
                        isCount:(BOOL)aIsCount
                     completion:(void (^)(BOOL success))aCompletion;
                  
 //示例代码
 [[EaseHttpManager sharedInstance] leaveLiveRoomWithRoomId:@"直播室ID"
                                                chatroomId:@"聊天室ID"
                                                   isCount:YES
                                                completion:^(BOOL success) {
                                                }];

1. 发送消息。

/*!
 *  \~chinese
 *  发送消息
 *
 *  @param aMessage         消息
 *  @param aProgressBlock   附件上传进度回调block
 *  @param aCompletionBlock 发送完成回调block
 *
 *  \~english
 *  Send a message
 *
 *
 *  @param aMessage            Message instance
 *  @param aProgressBlock      The block of attachment upload progress
 *  @param aCompletionBlock    The block of send complete
 */
- (void)sendMessage:(EMMessage *)aMessage
           progress:(void (^)(int progress))aProgressBlock
         completion:(void (^)(EMMessage *message, EMError *error))aCompletionBlock;

//示例代码
//文本消息
EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:@"发送内容"];
NSString *from = [[EMClient sharedClient] currentUsername];
EMMessage *message = [[EMMessage alloc] initWithConversationID:aChatroomId from:from to:aChatroomId body:body ext:nil];
message.chatType = EMChatTypeChatRoom;
[[EMClient sharedClient].chatManager sendMessage:message progress:nil completion:^(EMMessage *message, EMError *error) {
	if (!error) {
	//消息发送成功
	}
}]; 

您可以使用自定义消息体消息来实现礼物,点赞,弹幕等聊天室常见功能。

/*
发送自定义消息(礼物,点赞,弹幕)
@param text                 消息内容
@param num                  消息内容数量
@param to                   消息发送对象
@param messageType          聊天类型
@param aCompletionBlock     发送完成回调block
*/
- (void)sendCustomMessage:(NSString*)text
                              num:(NSInteger)num
                               to:(NSString*)toUser
                    customMsgType:(customMessageType)customMsgType
                       completion:(void (^)(EMMessage *message, EMError *error))aCompletionBlock;
{
    EMMessageBody *body;
    NSMutableDictionary *extDic = [[NSMutableDictionary alloc]init];//自定义消息体的“事件”对应的参数集
    if (@“自定义消息体类型是点赞消息”) {
        [extDic setObject:[NSString stringWithFormat:@"%ld",(long)num] forKey:@"num"];
        body = [[EMCustomMessageBody alloc]initWithEvent:@“点赞消息体事件名称” ext:extDic];
    } else if (@“自定义消息体类型是礼物消息”){
        [extDic setObject:text forKey:@"id"];
        [extDic setObject:[NSString stringWithFormat:@"%ld",(long)num] forKey:@"num"];
        body = [[EMCustomMessageBody alloc]initWithEvent:@“礼物消息体事件名称” ext:extDic];
    } else if (@“自定义消息体类型是弹幕消息”) {
        [extDic setObject:text forKey:@"txt"];
        body = [[EMCustomMessageBody alloc]initWithEvent:@“弹幕消息体事件名称” ext:extDic];
    }
    NSString *from = [[EMClient sharedClient] currentUsername];
    EMMessage *message = [[EMMessage alloc] initWithConversationID:toUser from:from to:toUser body:body ext:nil];
    message.chatType = messageType;
    [[EMClient sharedClient].chatManager sendMessage:message progress:NULL completion:^(EMMessage *message, EMError *error) {
        aCompletionBlock(message,error);//发送消息完成的回调
    }];
}

请参考消息收发

2. 设置消息监听。

//示例代码
 [[EMClient sharedClient].chatManager addDelegate:self delegateQueue:nil];
 
 
- (void)messagesDidReceive:(NSArray *)aMessages
{
  //收到文本消息体消息

  //收到自定义消息体消息
  for (EMMessage *message in aMessages) {
       if (@“消息体类型是自定义消息体”) {
             EMCustomMessageBody* body = (EMCustomMessageBody*)message.body;
             if ([body.event isEqualToString:@“自定义消息体类型是弹幕消息”]) {
                 //弹幕消息
             } else if ([body.event isEqualToString:@“自定义消息体类型是点赞消息”]) {
                 //点赞消息
             } else if ([body.event isEqualToString:@“自定义消息体类型是礼物消息”]) {
                 //礼物消息
             }
        }
}

- (void)cmdMessagesDidReceive:(NSArray *)aCmdMessages
{
//收到cmd消息
}

1. 获取直播聊天室,这里指的是获取正在直播的直播聊天室列表。(后台状态是正在直播的直播聊天室,只要后台的状态是正在直播,就会显示在这个列表里面。)

//EaseHttpManager.h

/*
 *  获取正在直播聊天室列表
 *
 *  @param aCursor          游标
 *  @param aLimit           预期获取的记录数
 *  @param aCompletion      完成的回调block
 */
- (void)fetchLiveRoomsOngoingWithCursor:(NSString*)aCursor
                                  limit:(NSInteger)aLimit
                             completion:(void (^)(EMCursorResult *result, BOOL success))aCompletion;
                             
//示例代码
[[EaseHttpManager sharedInstance] fetchLiveRoomsOngoingWithCursor:nil
                                                            limit:8
                                                      completion:^(EMCursorResult *result, BOOL success) {
      //结果集里包含所请求数量的直播聊天室,并且每个直播聊天室拥有自己的封面图url、直播聊天室主题、描述信息等
                                                      }];

2. 获取聊天室成员列表。

/*!
 *  \~chinese
 *  获取聊天室成员列表
 *
 *  @param aChatroomId      聊天室ID
 *  @param aCursor          游标,首次调用传空
 *  @param aPageSize        获取多少条
 *  @param aCompletionBlock 完成的回调
 */
- (void)getChatroomMemberListFromServerWithId:(NSString *)aChatroomId
                                       cursor:(NSString *)aCursor
                                     pageSize:(NSInteger)aPageSize
                                   completion:(void (^)(EMCursorResult *aResult, EMError *aError))aCompletionBlock;
//示例代码
 [[EMClient sharedClient].roomManager getChatroomMemberListFromServerWithId:chatroomId
                                            cursor:nil
                                          pageSize:10
                                        completion:^(EMCursorResult *aResult, EMError *aError) {
                                        //aResult.list集合即是聊天室成员列表
                                                                    }];

3. 获取聊天室白名单列表。

/*!
 *  \~chinese
 *  获取聊天室白名单列表
 *
 *  @param aChatroomId      聊天室ID
 *  @param aCompletionBlock 完成的回调
 *
 */
- (void)getChatroomWhiteListFromServerWithId:(NSString *)aChatroomId
                                  completion:(void (^)(NSArray *aList, EMError *aError))aCompletionBlock;
                                  
//示例代码
[[EMClient sharedClient].roomManager getChatroomWhiteListFromServerWithId:@“聊天室id” completion:^(NSArray *aList, EMError *aError) {
        //aList即是当前所在聊天室白名单列表
    }];

4. 获取聊天室已禁言列表。

/*!
 *  \~chinese
 *  获取聊天室已禁言列表
 *
 *  @param aChatroomId      聊天室ID
 *  @param aPageNum         获取第几页
 *  @param aPageSize        获取多少条
 *  @param aCompletionBlock 完成的回调
 *
 */
- (void)getChatroomMuteListFromServerWithId:(NSString *)aChatroomId
                                 pageNumber:(NSInteger)aPageNum
                                   pageSize:(NSInteger)aPageSize
                                 completion:(void (^)(NSArray *aList, EMError *aError))aCompletionBlock;
                                  
//示例代码@
[[EMClient sharedClient].roomManager getChatroomMuteListFromServerWithId:@“聊天室id”
                                                                  pageNumber:@“当前页”
                                                                    pageSize:10
                                                                  completion:^(NSArray *aList, EMError *aError) {
                 //aList即是当前所在聊天室已禁言列表
                                                                  }];

5. 修改直播聊天室状态,当直播结束的时候,主动修改直播聊天室的状态为Offline未直播态,避免在直播列表里面显示。

//EaseHttpManager.h

/*
*  更新直播间状态为offline
*
*  @param aRoom            直播间
*  @param aCompletion      完成的回调block
*/
- (void)modifyLiveroomStatusWithOffline:(EaseLiveRoom *)room
                             completion:(void (^)(EaseLiveRoom *room, BOOL success))aCompletion;

//示例代码
[[EaseHttpManager sharedInstance] modifyLiveroomStatusWithOffline:@“当前直播聊天室对象” completion:^(EaseLiveRoom *room, BOOL success) {
    }];

6. 修改直播聊天室详情

//EaseHttpManager.h

/*
 *  修改直播聊天室详情(封面图/主题/描述信息等)
 *
 *  @param aRoom            直播聊天室
 *  @param aCompletion      完成的回调block
 */
- (void)modifyLiveRoomWithRoom:(EaseLiveRoom*)aRoom
                    completion:(void (^)(EaseLiveRoom *room, BOOL success))aCompletion;

//示例代码
[[EaseHttpManager sharedInstance] modifyLiveRoomWithRoom:@“当前直播聊天室对象” completion:^(EaseLiveRoom *aRoom, BOOL success) {
            }];

1. 禁言和解除禁言,需要直播的群主和管理员有权限。

// IEMChatroomManager

/*!
 *  将一组成员禁言,需要Owner / Admin权限
 *
 *  同步方法,会阻塞当前线程
 *
 *  @param aMuteMembers         要禁言的成员列表<NSString>
 *  @param aMuteMilliseconds    禁言时长
 *  @param aChatroomId          聊天室ID
 *  @param pError               错误信息
 *
 *  @result    聊天室实例
 */
- (EMChatroom *)muteMembers:(NSArray *)aMuteMembers
           muteMilliseconds:(NSInteger)aMuteMilliseconds
               fromChatroom:(NSString *)aChatroomId
                      error:(EMError **)pError;

//示例代码
EMError *error = nil;
[[EMClient sharedClient].roomManager muteMembers:@[@"用户ID"] muteMilliseconds:-1 fromChatroom:@"聊天室ID" error:&error];

/*!
 *  解除禁言,需要Owner / Admin权限
 *
 *  同步方法,会阻塞当前线程
 *
 *  @param aMuteMembers     被解除的列表<NSString>
 *  @param aChatroomId      聊天室ID
 *  @param pError           错误信息
 *
 *  @result    聊天室实例
 */
- (EMChatroom *)unmuteMembers:(NSArray *)aMembers
                 fromChatroom:(NSString *)aChatroomId
                        error:(EMError **)pError;

//示例代码
EMError *error = nil;
[[EMClient sharedClient].roomManager unmuteMembers:@[@"用户ID"] fromChatroom:@"聊天室ID" error:&error];

2. 加入白名单,从白名单移除,需要直播的群主有权限。

/*!
 *  \~chinese
 *  添加白名单,需要Owner / Admin权限
 *
 *  @param aMembers         被添加的列表<NSString>
 *  @param aChatroomId      聊天室ID
 *  @param aCompletionBlock 完成的回调
 */
- (void)addWhiteListMembers:(NSArray *)aMembers
               fromChatroom:(NSString *)aChatroomId
                 completion:(void (^)(EMChatroom *aChatroom, EMError *aError))aCompletionBlock;

//示例代码
[[EMClient sharedClient].roomManager addWhiteListMembers:@[@“观众1”,@“观众2”] fromChatroom:@“聊天室id” completion:^(EMChatroom *aChatroom, EMError *aError) {
        }];                                           

/*!
 *  \~chinese
 *  移除白名单,需要Owner / Admin权限
 *
 *  @param aMembers         被移除的列表<NSString>
 *  @param aChatroomId      聊天室ID
 *  @param aCompletionBlock 完成的回调
 */
- (void)removeWhiteListMembers:(NSArray *)aMembers
                  fromChatroom:(NSString *)aChatroomId
                    completion:(void (^)(EMChatroom *aChatroom, EMError *aError))aCompletionBlock;

//示例代码
[[EMClient sharedClient].roomManager removeWhiteListMembers:@[@[@“成员1”,@“成员2”]] fromChatroom:@“聊天室id” completion:^(EMChatroom *aChatroom, EMError *aError) {
        }];

3. 聊天室踢人,需要直播的群主和管理员的权限。

/*!
 *  将成员移出聊天室, 需要owner/admin权限
 *
 *  @param aMembers         要移出的用户列表
 *  @param aChatroomId      聊天室ID
 *  @param aCompletionBlock 完成的回调
 */
- (void)removeMembers:(NSArray *)aMembers
         fromChatroom:(NSString *)aChatroomId
           completion:(void (^)(EMChatroom *aChatroom, EMError *aError))aCompletionBlock;

//示例代码
[[EMClient sharedClient].roomManager removeMembers:@[@"用户ID"]
                                      fromChatroom:@"聊天室ID"
                                        completion:^(EMChatroom *aChatroom, EMError *aError) {
                                        }];       

4. 设置聊天室全员禁言/解除全员禁言,需要Owner / Admin权限。

/*!
 *  \~chinese
 *  设置全员禁言,需要Owner / Admin权限
 *
 *  同步方法,会阻塞当前线程
 *
 *  @param aChatroomId      聊天室ID
 *  @param pError           错误信息
 *
 *  @result    聊天室实例
 */
- (EMChatroom *)muteAllMembersFromChatroom:(NSString *)aChatroomId
                                     error:(EMError **)pError;

//示例代码
[[EMClient sharedClient].roomManager muteAllMembersFromChatroom:@“聊天室id” completion:^(EMChatroom *aChatroom, EMError *aError) {
        }];     

/*!
 *  \~chinese
 *  解除全员禁言,需要Owner / Admin权限
 *
 *  @param aChatroomId      聊天室ID
 *  @param aCompletionBlock 完成的回调
 */
- (void)unmuteAllMembersFromChatroom:(NSString *)aChatroomId
                          completion:(void(^)(EMChatroom *aChatroom, EMError *aError))aCompletionBlock;

//示例代码
[[EMClient sharedClient].roomManager unmuteAllMembersFromChatroom:_chatroomId completion:^(EMChatroom *aChatroom, EMError *aError) {
        }];

5. 观众的加入聊天室和离开聊天室。

//注册聊天室的回调
[[EMClient sharedClient].roomManager addDelegate:self delegateQueue:nil];

- (void)userDidJoinChatroom:(EMChatroom *)aChatroom
                       user:(NSString *)aUsername
{
//观众加入聊天室
}

- (void)userDidLeaveChatroom:(EMChatroom *)aChatroom
                        user:(NSString *)aUsername
{
//观众离开聊天室
}