环信 聊天
框架
App
Easy UI (UI控件)
SDK 基于核心协议实现的完整的 IM 功能,实现了不同类型消息的收发、会话管理、群组、好友、聊天室等功能
SDK_CORE 为核心的消息同步协议实现,完成与服务器之间的信息交换
SDK
EMClient SDK的入口,主要完成登录、退出、连接管理等功能,也是获取其他模块的入口。
EMClientManager 管理消息的收发,完成会话管理等功能
EMContactManager 负责好友的添加删除,黑名单的管理
EMGroupManager 负责群组的管理,创建、删除群组,管理群组成员等功能
EMChatroomManager 负责聊天室的管理
配置
1. 苹果开发官网创建 2推送证书(开发证书 和 线上证书)
2. 环信开发者中心注册应用(添加上述2推送证书)(获取KEY)
pod 'Hyphenate' 包含实时语音功能
pod 'EaseUI' 快速集成UI
#import <Hyphenate/Hyphenate.h>
AppDelegate
// 在环信中注册本应用
EMOptions *options=[EMOptions optionsWithAppkey:@"64619039#kachamao"];
// 证书
#if DEBUG
[options setApnsCertName:@"s_dev"];
#else
[options setApnsCertName:@"s_product"];
#endif
[[EMClient sharedClient]initializeSDKWithOptions:options];
// APP进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application{
// 环信进入后台
[[EMClient sharedClient] applicationDidEnterBackground:application];
}
// APP将要从后台返回前台
- (void)applicationWillEnterForeground:(UIApplication *)application{
// 环信即将进入前台
[[EMClient sharedClient] applicationWillEnterForeground:application];
}
注册登录
注册
EMError *error = [[EMClient sharedClient] registerWithUsername:@"唯一表示用户" password:@"111111"];
if (error==nil) {
NSLog(@"注册成功");
}
登陆
[[EMClient sharedClient] loginWithUsername:@"8001"
password:@"111111"
completion:^(NSString *aUsername, EMError *aError) {
if (!aError) {
NSLog(@"登录成功");
} else {
NSLog(@"登录失败");
}
}];
// 以下情况会导致自动登录失败
1.用户调用了 SDK 的登出动作;
2.用户在别的设备上更改了密码,导致此设备上自动登录失败;
3.用户的账号被从服务器端删除;
4.用户从另一个设备登录,把当前设备上登录的用户踢出。
自动登录
BOOL isAutoLogin = [EMClient sharedClient].options.isAutoLogin;
if (!isAutoLogin) {
EMError *error = [[EMClient sharedClient] loginWithUsername:@"8001" password:@"111111"];
if (!error)
{
[[EMClient sharedClient].options setIsAutoLogin:YES];
}
}
// 自动登录后调用
- (void)autoLoginDidCompleteWithError:(EMError *)error{}
// 重连时回调
- (void)connectionStateDidChange:(EMConnectionState)aConnectionState{}
退出登录
-》主动退出登录:调用 SDK 的退出接口;
-》被动退出登录:1. 正在登录的账号在另一台设备上登录;2. 正在登录的账号被从服务器端删除。
// 主动退出登录
EMError *error = [[EMClient sharedClient] logout:YES];
if (!error) {
NSLog(@"退出成功");
}
// 当用户从另一个设备登录此账号时调用
- (void)userAccountDidLoginFromOtherDevice{}
// 当用户的账号被从服务器端删除时调用
- (void)userAccountDidRemoveFromServer{}
好友
// 从服务器获取所有的好友
[[EMClient sharedClient].contactManager getContactsFromServerWithCompletion:^(NSArray *aList, EMError *aError) {
if (!aError) {
NSLog(@"获取成功");
}
}];
// 从数据库获取所有的好友
NSArray *userlist = [[EMClient sharedClient].contactManager getContacts];
// 发送 添加好友 申请
[[EMClient sharedClient].contactManager addContact:@"8001"
message:@"我想加您为好友"
completion:^(NSString *aUsername, EMError *aError) {
if (!aError) {
NSLog(@"邀请发送成功");
}
}];
// 同意好友申请
[[EMClient sharedClient].contactManager approveFriendRequestFromUser:@"8001"
completion:^(NSString *aUsername, EMError *aError) {
if (!aError) {
NSLog(@"同意好友成功");
}
}];
// 拒绝好友申请
[[EMClient sharedClient].contactManager declineFriendRequestFromUser:@"8001"
completion:^(NSString *aUsername, EMError *aError) {
if (!aError) {
NSLog(@"拒绝好友成功");
}
}];
// 删除好友
[[EMClient sharedClient].contactManager deleteContact:@"8001"
isDeleteConversation: YES
completion:^(NSString *aUsername, EMError *aError) {
if (!aError) {
NSLog(@"删除成功");
}
}];
// 注册好友dele <EMContactManagerDelegate>
[[EMClient sharedClient].contactManager addDelegate:self delegateQueue:nil];
// 移除dele
[[EMClient sharedClient].contactManager removeDelegate:self];
// 对方同意加好友请求后回调
- (void)friendRequestDidApproveByUser:(NSString *)aUsername{}
// 对方拒绝加好友请求后回调
- (void)friendRequestDidDeclineByUser:(NSString *)aUsername{}
// 对方删除我,双方都回调
- (void)friendshipDidRemoveByUser:(NSString *)aUsername{}
// 对方同意好友请求后,双方回调
- (void)friendshipDidAddByUser:(NSString *)aUsername{}
// 对方发送好友请求后回调
- (void)friendRequestDidReceiveFromUser:(NSString *)aUsername message:(NSString *)aMessage{}
// 获取好友黑名单
EMError *error = nil;
NSArray *blackList = [[EMClient sharedClient].contactManager getBlackListFromServerWithError:&error];
if (!error) {
NSLog(@"获取成功 -- %@",blackList);
}
// 从数据库获取黑名单列表
NSArray *blockList = [[EMClient sharedClient].contactManager getBlackList];
// 加入黑名单
EMError *error = [[EMClient sharedClient].contactManager addUserToBlackList:@"6001" relationshipBoth:YES];
if (!error) {
NSLog(@"发送成功");
}
// 移除黑名单
EMError *error = [[EMClient sharedClient].contactManager removeUserFromBlackList:@"6001"];
if (!error) {
NSLog(@"发送成功");
}
会话
获取所有会话(内存中有则从内存中取,没有则从db中取)
NSArray *conversations = [[EMClient sharedClient].chatManager getAllConversations];
获取或创建会话
EMConversation *conversation = [[EMClient sharedClient].chatManager getConversation:@"8001" type:EMConversationTypeChat createIfNotExist:YES];
EMConversationTypeChat 单聊会话
EMConversationTypeGroupChat 群聊会话
EMConversationTypeChatRoom 聊天室会话
删除会话
[[EMClient sharedClient].chatManager deleteConversation:@"8001" isDeleteMessages:YES completion:^(NSString *aConversationId, EMError *aError){
//code
}];
根据 conversationId 批量删除会话
EMConversation *conversation = [[EMClient sharedClient].chatManager getConversation:@"8001" type:EMConversationTypeChat createIfNotExist:NO];
[[EMClient sharedClient].chatManager deleteConversations:@[conversation] isDeleteMessages:YES completion:^(EMError *aError){
//code
}];
获取会话未读消息数
[EMConversation unreadMessagesCount];
获取会话中的消息
// 从数据库获取时间段内的消息(时间单位:毫秒),最大数量10
[conversion loadMessagesFrom:1000 to:1000 count:10 completion:^(NSArray *aMessages, EMError *aError) {
// 获取完毕后回调
}];
// 从数据库获取指定id的消息
[conversion loadMessageWithId:@"messageId" error:nil];
// 从数据库获取从指定id的消息(不包含)后开始10条消息
[conversion loadMessagesStartFromId:@"messageId" count:10 searchDirection:EMMessageSearchDirectionUp completion:^(NSArray *aMessages, EMError *aError) {
}];
// 从数据库获取从指定时间(毫秒)指定发送人,从上到下10条消息
[conversion loadMessagesWithType:EMMessageBodyTypeText timestamp:1000 count:10 fromUser:@"" searchDirection:EMMessageSearchDirectionUp completion:^(NSArray *aMessages, EMError *aError) {
}];
// 从数据库获取从指定时间(毫秒)指定发送人包含关键字,从上到下10条消息
[conversion loadMessagesWithKeyword:@"keyWord" timestamp:1000 count:10 fromUser:@"" searchDirection:EMMessageSearchDirectionUp completion:^(NSArray *aMessages, EMError *aError) {
}];
消息
1.消息体
文本消息
EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:@"要发送的消息"];
图片消息
EMImageMessageBody *body = [[EMImageMessageBody alloc] initWithData:data displayName:@"image.png"];
位置消息
EMLocationMessageBody *body = [[EMLocationMessageBody alloc] initWithLatitude:39 longitude:116 address:@"地址"];
语音消息
EMVoiceMessageBody *body = [[EMVoiceMessageBody alloc] initWithLocalPath:@"audioPath" displayName:@"audio"];
body.duration = duration;
视频消息
EMVideoMessageBody *body = [[EMVideoMessageBody alloc] initWithLocalPath:@"videoPath" displayName:@"video.mp4"];
文件消息
EMFileMessageBody *body = [[EMFileMessageBody alloc] initWithLocalPath:@"filePath" displayName:@"file"];
透传消息
EMCmdMessageBody *body = [[EMCmdMessageBody alloc] initWithAction:action];
2.消息
// 消息
NSString *from = [[EMClient sharedClient] currentUsername];
EMMessage *message = [[EMMessage alloc] initWithConversationID:@"6001" from:from to:@"6001" body:body ext:messageExt];
message.chatType = EMChatTypeChat; // 单聊,EMChatTypeGroupChat群聊 EMChatTypeChatRoom聊天室
message.ext = @{@"key":@"value"}; // 扩展消息
3.插入消息
[[EMClient sharedClient].chatManager importMessages:@[message] completion:^(EMError *aError) {
//code
}];
3.发送消息(异步)
[[EMClient sharedClient].chatManager sendMessage:[EMMessage new] progress:^(int progress) {
} completion:^(EMMessage *message, EMError *error) {
}];
// 更新消息到 DB
[[EMClient sharedClient].chatManager updateMessage:aMessage];
// 添加dele <EMChatManagerDelegate>
[[EMClient sharedClient].chatManager addDelegate:self delegateQueue:nil];
// 移除dele
[[EMClient sharedClient].chatManager removeDelegate:self];
// 收到消息(非透传)后回调
- (void)messagesDidReceive:(NSArray *)aMessages{
//
for (EMMessage *message in aMessages) {
// 消息中的扩展属性
NSDictionary *ext = message.ext;
EMMessageBody *msgBody = message.body;
switch (msgBody.type) {
case EMMessageBodyTypeText:{
// 收到的文字消息
EMTextMessageBody *textBody = (EMTextMessageBody *)msgBody;
NSString *txt = textBody.text;
NSLog(@"收到的文字是 txt -- %@",txt);
}
break;
case EMMessageBodyTypeImage:{
// 得到一个图片消息body
EMImageMessageBody *body = ((EMImageMessageBody *)msgBody);
NSLog(@"大图remote路径 -- %@" ,body.remotePath);
NSLog(@"大图local路径 -- %@" ,body.localPath); // // 需要使用sdk提供的下载方法后才会存在
NSLog(@"大图的secret -- %@" ,body.secretKey);
NSLog(@"大图的W -- %f ,大图的H -- %f",body.size.width,body.size.height);
NSLog(@"大图的下载状态 -- %lu",body.downloadStatus);
// 缩略图sdk会自动下载
NSLog(@"小图remote路径 -- %@" ,body.thumbnailRemotePath);
NSLog(@"小图local路径 -- %@" ,body.thumbnailLocalPath);
NSLog(@"小图的secret -- %@" ,body.thumbnailSecretKey);
NSLog(@"小图的W -- %f ,大图的H -- %f",body.thumbnailSize.width,body.thumbnailSize.height);
NSLog(@"小图的下载状态 -- %lu",body.thumbnailDownloadStatus);
}
break;
case EMMessageBodyTypeLocation:{
EMLocationMessageBody *body = (EMLocationMessageBody *)msgBody;
NSLog(@"纬度-- %f",body.latitude);
NSLog(@"经度-- %f",body.longitude);
NSLog(@"地址-- %@",body.address);
}
break;
case EMMessageBodyTypeVoice:{
// 音频sdk会自动下载
EMVoiceMessageBody *body = (EMVoiceMessageBody *)msgBody;
NSLog(@"音频remote路径 -- %@" ,body.remotePath);
NSLog(@"音频local路径 -- %@" ,body.localPath); // 需要使用sdk提供的下载方法后才会存在(音频会自动调用)
NSLog(@"音频的secret -- %@" ,body.secretKey);
NSLog(@"音频文件大小 -- %lld" ,body.fileLength);
NSLog(@"音频文件的下载状态 -- %lu" ,body.downloadStatus);
NSLog(@"音频的时间长度 -- %lu" ,body.duration);
}
break;
case EMMessageBodyTypeVideo:{
EMVideoMessageBody *body = (EMVideoMessageBody *)msgBody;
NSLog(@"视频remote路径 -- %@" ,body.remotePath);
NSLog(@"视频local路径 -- %@" ,body.localPath); // 需要使用sdk提供的下载方法后才会存在
NSLog(@"视频的secret -- %@" ,body.secretKey);
NSLog(@"视频文件大小 -- %lld" ,body.fileLength);
NSLog(@"视频文件的下载状态 -- %lu" ,body.downloadStatus);
NSLog(@"视频的时间长度 -- %lu" ,body.duration);
NSLog(@"视频的W -- %f ,视频的H -- %f", body.thumbnailSize.width, body.thumbnailSize.height);
// 缩略图sdk会自动下载
NSLog(@"缩略图的remote路径 -- %@" ,body.thumbnailRemotePath);
NSLog(@"缩略图的local路径 -- %@" ,body.thumbnailLocalPath);
NSLog(@"缩略图的secret -- %@" ,body.thumbnailSecretKey);
NSLog(@"缩略图的下载状态 -- %lu" ,body.thumbnailDownloadStatus);
}
break;
case EMMessageBodyTypeFile:{
EMFileMessageBody *body = (EMFileMessageBody *)msgBody;
NSLog(@"文件remote路径 -- %@" ,body.remotePath);
NSLog(@"文件local路径 -- %@" ,body.localPath); // 需要使用sdk提供的下载方法后才会存在
NSLog(@"文件的secret -- %@" ,body.secretKey);
NSLog(@"文件文件大小 -- %lld" ,body.fileLength);
NSLog(@"文件文件的下载状态 -- %lu" ,body.downloadStatus);
}
break;
default:
break;
}
}
}
// 收到透传(cmd)在线消息后回调
- (void)cmdMessagesDidReceive:(NSArray *)aCmdMessages{
for (EMMessage *message in aCmdMessages) {
// cmd消息中的扩展属性
NSDictionary *ext = message.ext;
EMCmdMessageBody *body = (EMCmdMessageBody *)message.body;
NSLog(@"收到的action是 -- %@",body.action);
}
}
// 会话列表变动后调用
- (void)conversationListDidUpdate:(NSArray *)aConversationList{}
// 收到消息后调用
- (void)messagesDidReceive:(NSArray *)aMessages{}
// 收到cmd透传消息后调用
- (void)cmdMessagesDidReceive:(NSArray *)aCmdMessages{}
// 消息已读后回调
- (void)messagesDidRead:(NSArray *)aMessages{}
// 消息送达后回调
- (void)messagesDidDeliver:(NSArray *)aMessages{}
// 消息撤回后回调
- (void)messagesDidRecall:(NSArray *)aMessages{}
// 消息状态改变时调用
- (void)messageStatusDidChange:(EMMessage *)aMessage error:(EMError *)aError{}
// 消息附件状态改变时调用
- (void)messageAttachmentStatusDidChange:(EMMessage *)aMessage error:(EMError *)aError{}
群组
群组类型(4种)
EMGroupStylePrivateOnlyOwnerInvite 私有群组,创建完成后,只允许 Owner 邀请用户加入
EMGroupStylePrivateMemberCanInvite 私有群组,创建完成后,只允许 Owner 和群成员邀请用户加入
EMGroupStylePublicJoinNeedApproval 公开群组,创建完成后,只允许 Owner 邀请用户加入; 非群成员用户需发送入群申请,Owner 同意后才能入组
EMGroupStylePublicOpenJoin 公开群组,创建完成后,允许非群组成员加入,不需要管理员同意
创建群组
EMError *error = nil;
EMGroupOptions *setting = [[EMGroupOptions alloc] init];
setting.maxUsersCount = 500;
setting.style = EMGroupStylePublicOpenJoin;// 创建不同类型的群组,这里需要才传入不同的类型
EMGroup *group = [[EMClient sharedClient].groupManager createGroupWithSubject:@"群组名称" description:@"群组描述" invitees:@[@"6001",@"6002"] message:@"邀请您加入群组" setting:setting error:&error];
if(!error){
NSLog(@"创建成功 -- %@",group);
}
获取群详情
// 获取群详情
EMGroup *group=[[EMClient sharedClient].groupManager getGroupSpecificationFromServerWithId:@"groupId" error:nil];
// 获取群详情
[[EMClient sharedClient].groupManager getGroupSpecificationFromServerWithId:@"" completion:^(EMGroup *aGroup, EMError *aError) {
}];
// 申请加入群组
EMError *error = nil;
[[EMClient sharedClient].groupManager applyJoinPublicGroup:@"groupId" message:@"" error:nil];
// 管理员 加人进群
EMError *error = nil;
[[EMClient sharedClient].groupManager addOccupants:@[@"user1"] toGroup:@"groupId" welcomeMessage:@"message" error:&error];
// 管理员 同意进群申请
EMError *error = [[EMClient sharedClient].groupManager acceptJoinApplication:@"groupId" applicant:@"user1"];
// 管理员 拒绝加群申请
EMError *error = [[EMClient sharedClient].groupManager declineJoinApplication:@"groupId" groupname:@"subject" applicant:@"user1" reason:@"拒绝的原因"];
// 加入公开群
MError *error = nil;
[[EMClient sharedClient].groupManager joinPublicGroup:@"1410329312753" error:&error];
// 主动退群
EMError *error = nil;
[[EMClient sharedClient].groupManager leaveGroup:@"1410329312753" error:&error];
// 解散群组
EMError *error = nil;
[[EMClient sharedClient].groupManager destroyGroup:@"groupId" error:&error];
if (!error) {
NSLog(@"解散成功");
}
// 修改群名称
EMError *error = nil;
[[EMClient sharedClient].groupManager changeGroupSubject:@"要修改的名称" forGroup:@"1410329312753" error:&error];
if (!error) {
NSLog(@"修改成功");
}
// 修改群描述
EMError *error = nil;
EMGroup* group = [[EMClient sharedClient].groupManager changeDescription:@"修改的群描述" forGroup:@"1410329312753" error:&error];
if (!error) {
NSLog(@"修改成功");
}
// 获取群成员列表
EMError *error = nil;
EMCursorResult* result = [[EMClient sharedClient].groupManager getGroupMemberListFromServerWithId:@"groupId" cursor:@"cursor" pageSize:50 error:&error];
if (!error) {
NSLog(@"获取成功");
// result.list: 返回的成员列表,内部的值是成员的环信id。
// result.cursor: 返回的cursor,如果要取下一页的列表,需要将这个cursor当参数传入到获取群组成员列表中。
}
// 异步
[[EMClient sharedClient].groupManager getGroupMemberListFromServerWithId:@"" cursor:@"" pageSize:50 completion:^(EMCursorResult *aResult, EMError *aError) {
}];
// 获取群黑名单列表
EMError *error = nil;
EMGroup* group = [[EMClient sharedClient].groupManager getGroupBlacklistFromServerWithId:@"groupId" pageNumber:1 pageSize:50 error:&error];
if (!error) {
NSLog(@"获取成功");
}
// 异步
[[EMClient sharedClient].groupManager getGroupBlacklistFromServerWithId:@"" pageNumber:@1 pageSize:@50 completion:^(NSArray *aList, EMError *aError) {
}];
// 获取被禁言列表
EMError *error = nil;
EMGroup* group = [[EMClient sharedClient].groupManager getGroupMuteListFromServerWithId:@"groupId" pageNumber:1 pageSize:50 error:&error];
if (!error) {
NSLog(@"获取成功");
}
// 异步
[[EMClient sharedClient].groupManager getGroupMuteListFromServerWithId:@"" pageNumber:1 pageSize:50 completion:^(NSArray *aList, EMError *aError) {
}];
// 获取群公告
[[EMClient sharedClient].groupManager getGroupAnnouncementWithId:@"groupId"
completion:^(NSString *aAnnouncement, EMError *aError) {
if (!aError) {
NSLog(@"获取成功");
}
}];
// 异步
[[EMClient sharedClient].groupManager updateGroupAnnouncementWithId:@"" announcement:@"" completion:^(EMGroup *aGroup, EMError *aError) {
}];
// 修改群公告
[[EMClient sharedClient].groupManager updateGroupAnnouncementWithId:@"groupid"
announcement:@"群公告"
completion:^(EMGroup *aGroup, EMError *aError) {
if (!aError) {
NSLog(@"修改成功");
}
}];
// 获取群共享文件列表
// 同步
NSArray *arr=[[EMClient sharedClient].groupManager getGroupFileListWithId:@"" pageNumber:1 pageSize:10 error:nil];
// 异步
[[EMClient sharedClient].groupManager getGroupFileListWithId:@"groupId"
pageNumber:1
pageSize:10
completion:^(NSArray *aList, EMError *aError) {
if (!aError) {
NSLog(@"获取成功");
}
}];
// 上传群文件
[[EMClient sharedClient].groupManager uploadGroupSharedFileWithId:@"groupId"
filePath:@"filePath"
progress:^(int progress){
} completion:^(EMGroupSharedFile *aSharedFile, EMError *aError) {
if (!aError) {
NSLog(@"上传成功");
}
}];
// 下载群文件
[[EMClient sharedClient].groupManager downloadGroupSharedFileWithId:@"groupId"
filePath:@"filePath"
sharedFileId:@"fileId"
progress:^(int progress) {
} completion:^(EMGroup *aGroup, EMError *aError) {
if (!aError) {
NSLog(@"下载成功");
}
}];
// 删除共享文件 同步
EMError *error = nil;
[[EMClient sharedClient].groupManager removeGroupSharedFileWithId:@"groupId" sharedFileId:@"fileId" error:&error];
if (!error) {
NSLog(@"删除成功");
}
// 异步
[[EMClient sharedClient].groupManager removeGroupSharedFileWithId:@"" sharedFileId:@"" completion:^(EMGroup *aGroup, EMError *aError) {
}];
// 修改群扩展信息(同步/异步)
[[EMClient sharedClient].groupManager updateGroupExtWithId:@"" ext:@"" error:nil];
[[EMClient sharedClient].groupManager updateGroupExtWithId:@"groupId"
ext:@"ext"
completion:^(EMGroup *aGroup, EMError *aError) {
if (!aError) {
NSLog(@"修改成功");
}
}];
// 改变群主(必须是群主)
EMError *error = nil;
[[EMClient sharedClient].groupManager updateGroupOwner:@"groupId" newOwner:@"newOwner" error:&error];
// 改变群主(异步)
[[EMClient sharedClient].groupManager updateGroupOwner:@"" newOwner:@"" completion:^(EMGroup *aGroup, EMError *aError) {
}];
// 添加群管理员
EMError *error = nil;
[[EMClient sharedClient].groupManager addAdmin:@"adminName" toGroup:@"groupId" error:&error];
[[EMClient sharedClient].groupManager addAdmin:@"" toGroup:@"" completion:^(EMGroup *aGroup, EMError *aError) {
}];
// 移除群管理员
EMError *error = nil;
[[EMClient sharedClient].groupManager removeAdmin:@"adminName" fromGroup:@"groupId" error:&error];
[[EMClient sharedClient].groupManager removeAdmin:@"" fromGroup:@"" completion:^(EMGroup *aGroup, EMError *aError) {
}];
// 移除群成员
EMError *error = nil;
[[EMClient sharedClient].groupManager removeOccupants:@[@"user1"] fromGroup:@"1410329312753" error:&error];
[[EMClient sharedClient].groupManager removeMembers:@[] fromGroup:@"" completion:^(EMGroup *aGroup, EMError *aError) {
}];
// 禁言群成员
EMError *error = nil;
[[EMClient sharedClient].groupManager muteMembers:@[userName] muteMilliseconds:-1 fromGroup:@"groupId" error:&error];
[[EMClient sharedClient].groupManager muteMembers:@"" muteMilliseconds:30 fromGroup:@"" completion:^(EMGroup *aGroup, EMError *aError) {
}];
// 解禁言群成员
EMError *error = nil;
[[EMClient sharedClient].groupManager unmuteMembers:@[userName] fromGroup:@"groupId" error:&error];
[[EMClient sharedClient].groupManager unmuteMembers:@[] fromGroup:@"" completion:^(EMGroup *aGroup, EMError *aError) {
}];
// 加入群黑名单
EMError *error = nil;
EMGroup *group = [[EMClient sharedClient].groupManager blockOccupants:@[@"user1"] fromGroup:@"1410329312753" error:&error];
// 移除群黑名单
EMError *error = nil;
EMGroup *group = [[EMClient sharedClient].groupManager unblockOccupants:@[@"user1"] forGroup:@"1410329312753" error:&error];
// 屏蔽/取消屏蔽 群消息
[[EMClient sharedClient].groupManager blockGroup:@"groupId" error:nil];
[[EMClient sharedClient].groupManager unblockGroup:@"groupId" error:nil];
// 内存中获取所有群组,第一次从数据库加载
NSArray *groupList = [[EMClient sharedClient].groupManager getJoinedGroups];
// 获取和登录者相关的群组
EMError *error = nil;
NSArray *myGroups = [[EMClient sharedClient].groupManager getJoinedGroupsFromServerWithPage:1 pageSize:50 error:&error];
if (!error) {
NSLog(@"获取成功 -- %@",myGroups);
}
// 获取公开群组
EMError *error = nil;
EMCursorResult *result = [[EMClient sharedClient].groupManager getPublicGroupsFromServerWithCursor:nil pageSize:50 error:&error];
if (!error) {
NSLog(@"获取成功 -- %@",result);
}
// 群组dele <EMGroupManagerDelegate>
[[EMClient sharedClient].groupManager addDelegate:self delegateQueue:nil];
// 移除群组dele
[[EMClient sharedClient].groupManager removeDelegate:self];
// 对方邀请我加入某群时调用
- (void)groupInvitationDidReceive:(NSString *)aGroupId inviter:(NSString *)aInviter message:(NSString *)aMessage{}
// 对方同意我的加群邀请时调用
- (void)groupInvitationDidAccept:(EMGroup *)aGroup invitee:(NSString *)aInvitee{}
// 对方拒绝我的加群邀请时调用
- (void)groupInvitationDidDecline:(EMGroup *)aGroup invitee:(NSString *)aInvitee reason:(NSString *)aReason{}
// 自动加入群后回调(对方邀请)(EMOptions的isAutoAcceptGroupInvitation为YES)
- (void)didJoinGroup:(EMGroup *)aGroup inviter:(NSString *)aInviter message:(NSString *)aMessage{}
// 离开群组后回调
- (void)didLeaveGroup:(EMGroup *)aGroup reason:(EMGroupLeaveReason)aReason{}
// 收到入群请求后回调(群的类型是EMGroupStylePublicJoinNeedApproval)
- (void)joinGroupRequestDidReceive:(EMGroup *)aGroup user:(NSString *)aUsername reason:(NSString *)aReason{}
// 申请入群被拒后回调(群的类型是EMGroupStylePublicJoinNeedApproval)
- (void)joinGroupRequestDidDecline:(NSString *)aGroupId reason:(NSString *)aReason{}
// 申请入群被同意后回调(群的类型是EMGroupStylePublicJoinNeedApproval)
- (void)joinGroupRequestDidApprove:(EMGroup *)aGroup{}
// 群组列表更新后回调
- (void)groupListDidUpdate:(NSArray *)aGroupList{}
// 有成员被加入禁言列表后调用
- (void)groupMuteListDidUpdate:(EMGroup *)aGroup addedMutedMembers:(NSArray *)aMutedMembers muteExpire:(NSInteger)aMuteExpire{}
// 有成员被移除禁言列表后调用
- (void)groupMuteListDidUpdate:(EMGroup *)aGroup removedMutedMembers:(NSArray *)aMutedMembers{}
// 有成员被加入管理员列表后调用
- (void)groupAdminListDidUpdate:(EMGroup *)aGroup addedAdmin:(NSString *)aAdmin{}
// 有成员被移除管理员列表后调用
- (void)groupAdminListDidUpdate:(EMGroup *)aGroup removedAdmin:(NSString *)aAdmin{}
// 群组创建者更新后回调
- (void)groupOwnerDidUpdate:(EMGroup *)aGroup newOwner:(NSString *)aNewOwner oldOwner:(NSString *)aOldOwner{}
// 有用户加入群组后回调
- (void)userDidJoinGroup:(EMGroup *)aGroup user:(NSString *)aUsername{}
// 有用户离开群组后调用
- (void)userDidLeaveGroup:(EMGroup *)aGroup user:(NSString *)aUsername{}
// 群公告更新后调用
- (void)groupAnnouncementDidUpdate:(EMGroup *)aGroup announcement:(NSString *)aAnnouncement{}
// 有用户上传文件后调用
- (void)groupFileListDidUpdate:(EMGroup *)aGroup addedSharedFile:(EMGroupSharedFile *)aSharedFile{}
// 有用户删除群共享文件
- (void)groupFileListDidUpdate:(EMGroup *)aGroup removedSharedFile:(NSString *)aFileId{}
群主
// 作为群主 收到入群申请时调用
- (void)didReceiveJoinGroupApplication:(EMGroup *)aGroup applicant:(NSString *)aApplicant reason:(NSString *)aReason{}
聊天页面
进入系统单聊页面
EaseMessageViewController *chatController = [[EaseMessageViewController alloc] initWithConversationChatter:@"8001" conversationType:EMConversationTypeChat];
进入系统群聊页面
EaseMessageViewController *chatController = [[EaseMessageViewController alloc] initWithConversationChatter:@"groupId" conversationType:EMConversationTypeGroupChat];
// 下载消息中的附件
[[EMClient sharedClient].chatManager downloadMessageThumbnail:message progress:^(int progress) {
} completion:^(EMMessage *message, EMError *error) {
}];
// 下载消息中的原始附件
[[EMClient sharedClient].chatManager downloadMessageAttachment:message progress:^(int progress) {
} completion:^(EMMessage *message, EMError *error) {
}];
// 发送已读回执(需要开发者手动调用)
[[EMClient sharedClient].chatManager sendMessageReadAck:message completion:^(EMMessage *aMessage, EMError *aError) {
if(!aError){
NSLog(@"send success");
}
}];
// 获取消息漫游
[[EMClient sharedClient].chatManager fetchHistoryMessagesFromServer:@"要获取漫游消息的Conversation id" conversationType:EMConversationTypeChat startMessageId:@"参考起始消息的ID" pageSize:10 error:nil];
// 获取消息漫游(异步)
[[EMClient sharedClient].chatManager asyncFetchHistoryMessagesFromServer:@"" conversationType:EMConversationTypeChat startMessageId:@"" pageSize:10 complation:^(EMCursorResult *aResult, EMError *aError) {
}];
// 撤回消息
[[EMClient sharedClient].chatManager recallMessage:message completion:^(EMMessage *aMessage, EMError *aError) {
}];
聊天室
// 获取聊天室
EMError *error = nil;
NSArray *list = [[EMClient sharedClient].roomManager getChatroomsFromServerWithPage:0 pageSize:50 error:&error];
[[EMClient sharedClient].roomManager getChatroomsFromServerWithPage:0 pageSize:10 completion:^(EMPageResult *aResult, EMError *aError) {
}];
// 加入聊天室
EMError *error = nil;
EMChatroom *chatroom = [[EMClient sharedClient].roomManager joinChatroom:@"chatroomId" error:&error];
[[EMClient sharedClient].roomManager joinChatroom:@"" completion:^(EMChatroom *aChatroom, EMError *aError) {
}];
// 移除聊天室
EMError *error = nil;
[[EMClient sharedClient].roomManager leaveChatroom:@"chatroomId" error:&error];
[[EMClient sharedClient].roomManager leaveChatroom:@"" completion:^(EMError *aError) {
}];
// 获取聊天室详情
EMError *error = nil;
EMChatroom *room = [[EMClient sharedClient].roomManager getChatroomSpecificationFromServerWithId:@"roomId"error:&error];
[[EMClient sharedClient].roomManager getChatroomSpecificationFromServerWithId:@"" completion:^(EMChatroom *aChatroom, EMError *aError) {
}];
// 获取聊天室成员列表
EMError *error = nil;
EMCursorResult *result = [[EMClient sharedClient].roomManager getChatroomMemberListFromServerWithId:@"roomId" cursor:@"cursor" pageSize:50 error:&error];
[[EMClient sharedClient].roomManager getChatroomMemberListFromServerWithId:@"" cursor:@"" pageSize:10 completion:^(EMCursorResult *aResult, EMError *aError) {
}];
// 获取聊天室黑名单
EMError *error = nil;
NSArray *list = [[EMClient sharedClient].roomManager getChatroomBlacklistFromServerWithId:@"roomId" pageNumber:1 pageSize:50 error:&error];
[[EMClient sharedClient].roomManager getChatroomBlacklistFromServerWithId:@"" pageNumber:0 pageSize:10 completion:^(NSArray *aList, EMError *aError) {
}];
// 获取聊天室禁言列表
EMError *error = nil;
NSArray *list = [[EMClient sharedClient].roomManager getChatroomMuteListFromServerWithId:@"roomId" pageNumber:1 pageSize:50 error:&error];
[[EMClient sharedClient].roomManager getChatroomMuteListFromServerWithId:@"" pageNumber:0 pageSize:10 completion:^(NSArray *aList, EMError *aError) {
}];
// 更新聊天室名称
EMError *error = nil;
EMChatroom *room = [[EMClient sharedClient].roomManager updateSubject:@"newSubject" forChatroom:@"roomId" error:&error];
[[EMClient sharedClient].roomManager updateSubject:@"" forChatroom:@"" completion:^(EMChatroom *aChatroom, EMError *aError) {
}];
// 更新聊天室说明
EMError *error = nil;
EMChatroom *room = [[EMClient sharedClient].roomManager updateDescription:@"newDes" forChatroom:@"roomId" error:&error];
[[EMClient sharedClient].roomManager getChatroomAnnouncementWithId:@"" completion:^(NSString *aAnnouncement, EMError *aError) {
}];
// 获取聊天室公告
NSString *content=[[EMClient sharedClient].roomManager getChatroomAnnouncementWithId:@"" error:nil];
[[EMClient sharedClient].roomManager getChatroomAnnouncementWithId:@"chatroomId"
completion:^(NSString *aAnnouncement, EMError *aError) {
if (!aError) {
NSLog(@"获取成功");
}
}];
// 更新聊天室公告
EMChatroom *room=[[EMClient sharedClient].roomManager updateChatroomAnnouncementWithId:@"" announcement:@"" error:nil];
[[EMClient sharedClient].roomManager updateChatroomAnnouncementWithId:@"chatroomId"
announcement:@"announcement"
completion:^(EMChatroom *aChatroom, EMError *aError) {
if (!aError) {
NSLog(@"修改成功");
}
}];
// 以下相关方法都有同步异步执行,就省略了
// 将某成员移除聊天室
EMError *error = nil;
EMChatroom *room = [[EMClient sharedClient].roomManager removeMembers:@[@"username"] fromChatroom:@"roomId" error:&error];
// 将某成员加入黑名单
EMError *error = nil;
EMChatroom *room = [[EMClient sharedClient].roomManager blockMembers:@[@"username"] fromChatroom:@"roomId" error:&error];
// 将某成员移除黑名单
EMError *error = nil;
EMChatroom *room = [[EMClient sharedClient].roomManager unblockMembers:@[@"username"] fromChatroom:@"roomId" error:&error];
// 改变聊天室创建者时调用
EMError *error = nil;
EMChatroom *room = [[EMClient sharedClient].roomManager updateChatroomOwner:@"roomId" newOwner:@"newOwner" error:&error];
// 添加聊天室管理员
EMError *error = nil;
EMChatroom *room = [[EMClient sharedClient].roomManager addAdmin:@"adminName" toChatroom:@"roomId" error:&error];
// 移除聊天室管理员
EMError *error = nil;
EMChatroom *room = [[EMClient sharedClient].roomManager removeAdmin:@"adminName" fromChatroom:@"roomId" error:&error];
// 禁言聊天室成员
EMError *error = nil;
EMChatroom *room = [[EMClient sharedClient].roomManager muteMembers:@[@"userName"] muteMilliseconds:100 aChatroomId:@"roomId" error:&error];
// 解禁言聊天室成员
EMError *error = nil;
EMChatroom *room = [[EMClient sharedClient].roomManager unmuteMembers:@[@"userName"] fromChatroom:@"roomId" error:&error];
// 添加dele <EMChatroomManagerDelegate>
[[EMClient sharedClient].roomManager addDelegate:self delegateQueue:nil];
// 移除dele
[[EMClient sharedClient].roomManager removeDelegate:self];
// 有用户加入聊天室后调用
- (void)userDidJoinChatroom:(EMChatroom *)aChatroom user:(NSString *)aUsername{}
// 有用户离开聊天室后调用
- (void)userDidLeaveChatroom:(EMChatroom *)aChatroom user:(NSString *)aUsername{}
// 被踢出聊天室后调用
- (void)didDismissFromChatroom:(EMChatroom *)aChatroom reason:(EMChatroomBeKickedReason)aReason{}
// 有成员被禁言时调用
- (void)chatroomMuteListDidUpdate:(EMChatroom *)aChatroom addedMutedMembers:(NSArray *)aMutes muteExpire:(NSInteger)aMuteExpire{}
// 有成员被移除禁言列表时调用
- (void)chatroomMuteListDidUpdate:(EMChatroom *)aChatroom removedMutedMembers:(NSArray *)aMutes{}
// 有成员被加入管理员列表时调用
- (void)chatroomAdminListDidUpdate:(EMChatroom *)aChatroom addedAdmin:(NSString *)aAdmin
// 有成员被移出管理员列表时调用
- (void)chatroomAdminListDidUpdate:(EMChatroom *)aChatroom removedAdmin:(NSString *)aAdmin
// 聊天室创建者更新时调用
- (void)chatroomOwnerDidUpdate:(EMChatroom *)aChatroom newOwner:(NSString *)aNewOwner oldOwner:(NSString *)aOldOwner{}
// 聊天室公告有更新时调用
- (void)chatroomAnnouncementDidUpdate:(EMChatroom *)aChatroom announcement:(NSString *)aAnnouncement{}
通话
// 1.option配置
EMCallOptions *options = [[EMClient sharedClient].callManager getCallOptions];
// 当对方不在线时,是否给对方发送离线消息和推送,并等待对方回应
options.isSendPushIfOffline = NO;
// 设置视频分辨率:自适应分辨率、352 * 288、640 * 480、1280 * 720
options.videoResolution = EMCallVideoResolutionAdaptive;
// 最大视频码率,范围 50 < videoKbps < 5000, 默认0, 0为自适应,建议设置为0
options.maxVideoKbps = 0;
// 最小视频码率
options.minVideoKbps = 0;
// 是否固定视频分辨率,默认为NO
options.isFixedVideoResolution = NO;
[[EMClient sharedClient].callManager setCallOptions:options];
// 2.发起通话
[[EMClient sharedClient].callManager startCall:EMCallTypeVideo remoteName:@"被呼叫的用户" ext:nil completion:^(EMCallSession *aCallSession, EMError *aError) {
if(!aError){
}
}];
// 2.同意通话
EMError *error = nil;
error = [[EMClient sharedClient].callManager answerIncomingCall:@"sessionId"];
// 3.结束通话
[[EMClient sharedClient].callManager endCall:@"sessionId" reason:EMCallEndReasonHangup];
/
EMCallEndReasonHangup = 0, 对方挂断
EMCallEndReasonNoResponse, 对方没有响应
EMCallEndReasonDecline, 对方拒接
EMCallEndReasonBusy, 对方占线
EMCallEndReasonFailed, 失败
EMCallEndReasonUnsupported, 功能不支持
EMCallEndReasonRemoteOffline, 对方不在线
/
//
EMCallSession *callSession=[EMCallSession new];
// 本地视频
callSession.localVideoView = [[EMCallLocalView alloc] initWithFrame:CGRectMake(0,0,0,0)];
[self.view addSubview:callSession.localVideoView];
// 对方视频
callSession.remoteVideoView = [[EMCallRemoteView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
// 缩放Mode
callSession.remoteVideoView.scaleMode = EMCallViewScaleModeAspectFill;
[self.view addSubview:_callSession.remoteVideoView];
// 暂停语音数据传输
[callSession pauseVideo];
// 恢复语音数据传输
[callSession resumeVideo];
// 暂停视图数据传输
[callSession pauseVideo];
// 恢复视频数据传输
[callSession resumeVideo];
// 是否使用前置摄像头,false:后置
[callSession switchCameraPosition:true];
// 添加dele <EMCallManagerDelegate>
[[EMClient sharedClient].callManager addDelegate:self delegateQueue:nil];
// 移除dele
[[EMClient sharedClient]removeDelegate:self];
// 收到视频请求后调用
- (void)callDidReceive:(EMCallSession *)aSession
// 统一通话后双方都回调
- (void)callDidConnect:(EMCallSession *)aSession
// 对方同意通话后回调
- (void)callDidAccept:(EMCallSession *)aSession
// 通话结束(包括出现错误)双方都回调
- (void)callDidEnd:(EMCallSession *)aSession reason:(EMCallEndReason)aReason error:(EMError *)aError{}
// 对方中断或者继续数据流传输时调用
- (void)callStateDidChange:(EMCallSession *)aSession type:(EMCallStreamingStatus)aType{}
// 网络状态不稳定时调用
- (void)callNetworkDidChange:(EMCallSession *)aSession status:(EMCallNetworkStatus)aStatus{}
// 离线dele <EMCallBuilderDelegate>
[[EMClient sharedClient].callManager setBuilderDelegate:self];
- (void)callRemoteOffline:(NSString *)aRemoteName{
//
NSString *text = [[EMClient sharedClient].callManager getCallOptions].offlineMessageText;
EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:text];
NSString *fromStr = [EMClient sharedClient].currentUsername;
EMMessage *message = [[EMMessage alloc] initWithConversationID:aRemoteName from:fromStr to:aRemoteName body:body ext:@{@"em_apns_ext":@{@"em_push_title":text}}];
message.chatType = EMChatTypeChat;
[[EMClient sharedClient].chatManager sendMessage:message progress:nil completion:nil];
}
多设备
// 获取当前帐号在其他设备上的ID列表
NSArray *otherPlatformIds = [[EMClient sharedClient].contactManager getSelfIdsOnOtherPlatformWithError:nil];
if ([otherPlatformIds count] > 0) {
NSString *chatter = otherPlatformIds[0];
//获取会话
EMConversation *conversation = [[EMClient sharedClient].chatManager getConversation:chatter type:EMConversationTypeChat createIfNotExist:YES];
//发送消息
NSString *sendText = @"test";
EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:sendText];
NSString *from = [[EMClient sharedClient] currentUsername];
EMMessage *message = [[EMMessage alloc] initWithConversationID:conversation.conversationId from:from to:chatter body:body ext:nil];
message.chatType = EMChatTypeChat;
[[EMClient sharedClient].chatManager sendMessage:message progress:nil completion:nil];
}
// <EMMultiDevicesDelegate>
[[EMClient sharedClient] addMultiDevicesDelegate:self delegateQueue:nil];
// 多设备收到好友时调用
- (void)multiDevicesContactEventDidReceive:(EMMultiDevicesEvent)aEvent
username:(NSString *)aTarget
ext:(NSString *)aExt{
NSString *message = [NSString stringWithFormat:@"%li-%@-%@", (long)aEvent, aTarget, aExt];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"alert.multi.contact", @"Contact Multi-devices") message:message delegate:self cancelButtonTitle:NSLocalizedString(@"ok", @"OK") otherButtonTitles:nil, nil];
[alertView show];
}
// 多设备收到群组时调用
- (void)multiDevicesGroupEventDidReceive:(EMMultiDevicesEvent)aEvent
groupId:(NSString *)aGroupId
ext:(id)aExt{
NSString *message = [NSString stringWithFormat:@"%li-%@-%@", (long)aEvent, aGroupId, aExt];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"alert.multi.group", @"Group Multi-devices") message:message delegate:self cancelButtonTitle:NSLocalizedString(@"ok", @"OK") otherButtonTitles:nil, nil];
[alertView show];
}
网友评论