環(huán)信 聊天
框架
App
Easy UI (UI控件)
SDK 基于核心協(xié)議實現(xiàn)的完整的 IM 功能,實現(xiàn)了不同類型消息的收發(fā)定拟、會話管理伦腐、群組缴淋、好友、聊天室等功能
SDK_CORE 為核心的消息同步協(xié)議實現(xiàn)吕座,完成與服務(wù)器之間的信息交換
SDK
EMClient SDK的入口盈匾,主要完成登錄忿薇、退出弦追、連接管理等功能岳链,也是獲取其他模塊的入口。
EMClientManager 管理消息的收發(fā)骗卜,完成會話管理等功能
EMContactManager 負責(zé)好友的添加刪除宠页,黑名單的管理
EMGroupManager 負責(zé)群組的管理,創(chuàng)建寇仓、刪除群組,管理群組成員等功能
EMChatroomManager 負責(zé)聊天室的管理
配置
1. 蘋果開發(fā)官網(wǎng)創(chuàng)建 2推送證書(開發(fā)證書 和 線上證書)
2. 環(huán)信開發(fā)者中心注冊應(yīng)用(添加上述2推送證書)(獲取KEY)
pod 'Hyphenate' 包含實時語音功能
pod 'EaseUI' 快速集成UI
#import <Hyphenate/Hyphenate.h>
AppDelegate
// 在環(huán)信中注冊本應(yīng)用
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{
// 環(huán)信進入后臺
[[EMClient sharedClient] applicationDidEnterBackground:application];
}
// APP將要從后臺返回前臺
- (void)applicationWillEnterForeground:(UIApplication *)application{
// 環(huán)信即將進入前臺
[[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(@"登錄失敗");
}
}];
// 以下情況會導(dǎo)致自動登錄失敗
1.用戶調(diào)用了 SDK 的登出動作烤宙;
2.用戶在別的設(shè)備上更改了密碼遍烦,導(dǎo)致此設(shè)備上自動登錄失敗躺枕;
3.用戶的賬號被從服務(wù)器端刪除服猪;
4.用戶從另一個設(shè)備登錄供填,把當(dāng)前設(shè)備上登錄的用戶踢出。
自動登錄
BOOL isAutoLogin = [EMClient sharedClient].options.isAutoLogin;
if (!isAutoLogin) {
EMError *error = [[EMClient sharedClient] loginWithUsername:@"8001" password:@"111111"];
if (!error)
{
[[EMClient sharedClient].options setIsAutoLogin:YES];
}
}
// 自動登錄后調(diào)用
- (void)autoLoginDidCompleteWithError:(EMError *)error{}
// 重連時回調(diào)
- (void)connectionStateDidChange:(EMConnectionState)aConnectionState{}
退出登錄
-》主動退出登錄:調(diào)用 SDK 的退出接口罢猪;
-》被動退出登錄:1. 正在登錄的賬號在另一臺設(shè)備上登錄近她;2. 正在登錄的賬號被從服務(wù)器端刪除。
// 主動退出登錄
EMError *error = [[EMClient sharedClient] logout:YES];
if (!error) {
NSLog(@"退出成功");
}
// 當(dāng)用戶從另一個設(shè)備登錄此賬號時調(diào)用
- (void)userAccountDidLoginFromOtherDevice{}
// 當(dāng)用戶的賬號被從服務(wù)器端刪除時調(diào)用
- (void)userAccountDidRemoveFromServer{}
好友
// 從服務(wù)器獲取所有的好友
[[EMClient sharedClient].contactManager getContactsFromServerWithCompletion:^(NSArray *aList, EMError *aError) {
if (!aError) {
NSLog(@"獲取成功");
}
}];
// 從數(shù)據(jù)庫獲取所有的好友
NSArray *userlist = [[EMClient sharedClient].contactManager getContacts];
// 發(fā)送 添加好友 申請
[[EMClient sharedClient].contactManager addContact:@"8001"
message:@"我想加您為好友"
completion:^(NSString *aUsername, EMError *aError) {
if (!aError) {
NSLog(@"邀請發(fā)送成功");
}
}];
// 同意好友申請
[[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];
// 對方同意加好友請求后回調(diào)
- (void)friendRequestDidApproveByUser:(NSString *)aUsername{}
// 對方拒絕加好友請求后回調(diào)
- (void)friendRequestDidDeclineByUser:(NSString *)aUsername{}
// 對方刪除我膳帕,雙方都回調(diào)
- (void)friendshipDidRemoveByUser:(NSString *)aUsername{}
// 對方同意好友請求后粘捎,雙方回調(diào)
- (void)friendshipDidAddByUser:(NSString *)aUsername{}
// 對方發(fā)送好友請求后回調(diào)
- (void)friendRequestDidReceiveFromUser:(NSString *)aUsername message:(NSString *)aMessage{}
// 獲取好友黑名單
EMError *error = nil;
NSArray *blackList = [[EMClient sharedClient].contactManager getBlackListFromServerWithError:&error];
if (!error) {
NSLog(@"獲取成功 -- %@",blackList);
}
// 從數(shù)據(jù)庫獲取黑名單列表
NSArray *blockList = [[EMClient sharedClient].contactManager getBlackList];
// 加入黑名單
EMError *error = [[EMClient sharedClient].contactManager addUserToBlackList:@"6001" relationshipBoth:YES];
if (!error) {
NSLog(@"發(fā)送成功");
}
// 移除黑名單
EMError *error = [[EMClient sharedClient].contactManager removeUserFromBlackList:@"6001"];
if (!error) {
NSLog(@"發(fā)送成功");
}
會話
獲取所有會話(內(nèi)存中有則從內(nèi)存中取,沒有則從db中取)
NSArray *conversations = [[EMClient sharedClient].chatManager getAllConversations];
獲取或創(chuàng)建會話
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
}];
根據(jù) conversationId 批量刪除會話
EMConversation *conversation = [[EMClient sharedClient].chatManager getConversation:@"8001" type:EMConversationTypeChat createIfNotExist:NO];
[[EMClient sharedClient].chatManager deleteConversations:@[conversation] isDeleteMessages:YES completion:^(EMError *aError){
//code
}];
獲取會話未讀消息數(shù)
[EMConversation unreadMessagesCount];
獲取會話中的消息
// 從數(shù)據(jù)庫獲取時間段內(nèi)的消息(時間單位:毫秒)危彩,最大數(shù)量10
[conversion loadMessagesFrom:1000 to:1000 count:10 completion:^(NSArray *aMessages, EMError *aError) {
// 獲取完畢后回調(diào)
}];
// 從數(shù)據(jù)庫獲取指定id的消息
[conversion loadMessageWithId:@"messageId" error:nil];
// 從數(shù)據(jù)庫獲取從指定id的消息(不包含)后開始10條消息
[conversion loadMessagesStartFromId:@"messageId" count:10 searchDirection:EMMessageSearchDirectionUp completion:^(NSArray *aMessages, EMError *aError) {
}];
// 從數(shù)據(jù)庫獲取從指定時間(毫秒)指定發(fā)送人攒磨,從上到下10條消息
[conversion loadMessagesWithType:EMMessageBodyTypeText timestamp:1000 count:10 fromUser:@"" searchDirection:EMMessageSearchDirectionUp completion:^(NSArray *aMessages, EMError *aError) {
}];
// 從數(shù)據(jù)庫獲取從指定時間(毫秒)指定發(fā)送人包含關(guān)鍵字,從上到下10條消息
[conversion loadMessagesWithKeyword:@"keyWord" timestamp:1000 count:10 fromUser:@"" searchDirection:EMMessageSearchDirectionUp completion:^(NSArray *aMessages, EMError *aError) {
}];
消息
1.消息體
文本消息
EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:@"要發(fā)送的消息"];
圖片消息
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.發(fā)送消息(異步)
[[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];
// 收到消息(非透傳)后回調(diào)
- (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(@"大圖的下載狀態(tài) -- %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(@"小圖的下載狀態(tài) -- %lu",body.thumbnailDownloadStatus);
}
break;
case EMMessageBodyTypeLocation:{
EMLocationMessageBody *body = (EMLocationMessageBody *)msgBody;
NSLog(@"緯度-- %f",body.latitude);
NSLog(@"經(jīng)度-- %f",body.longitude);
NSLog(@"地址-- %@",body.address);
}
break;
case EMMessageBodyTypeVoice:{
// 音頻sdk會自動下載
EMVoiceMessageBody *body = (EMVoiceMessageBody *)msgBody;
NSLog(@"音頻remote路徑 -- %@" ,body.remotePath);
NSLog(@"音頻local路徑 -- %@" ,body.localPath); // 需要使用sdk提供的下載方法后才會存在(音頻會自動調(diào)用)
NSLog(@"音頻的secret -- %@" ,body.secretKey);
NSLog(@"音頻文件大小 -- %lld" ,body.fileLength);
NSLog(@"音頻文件的下載狀態(tài) -- %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(@"視頻文件的下載狀態(tài) -- %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(@"縮略圖的下載狀態(tài) -- %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(@"文件文件的下載狀態(tài) -- %lu" ,body.downloadStatus);
}
break;
default:
break;
}
}
}
// 收到透傳(cmd)在線消息后回調(diào)
- (void)cmdMessagesDidReceive:(NSArray *)aCmdMessages{
for (EMMessage *message in aCmdMessages) {
// cmd消息中的擴展屬性
NSDictionary *ext = message.ext;
EMCmdMessageBody *body = (EMCmdMessageBody *)message.body;
NSLog(@"收到的action是 -- %@",body.action);
}
}
// 會話列表變動后調(diào)用
- (void)conversationListDidUpdate:(NSArray *)aConversationList{}
// 收到消息后調(diào)用
- (void)messagesDidReceive:(NSArray *)aMessages{}
// 收到cmd透傳消息后調(diào)用
- (void)cmdMessagesDidReceive:(NSArray *)aCmdMessages{}
// 消息已讀后回調(diào)
- (void)messagesDidRead:(NSArray *)aMessages{}
// 消息送達后回調(diào)
- (void)messagesDidDeliver:(NSArray *)aMessages{}
// 消息撤回后回調(diào)
- (void)messagesDidRecall:(NSArray *)aMessages{}
// 消息狀態(tài)改變時調(diào)用
- (void)messageStatusDidChange:(EMMessage *)aMessage error:(EMError *)aError{}
// 消息附件狀態(tài)改變時調(diào)用
- (void)messageAttachmentStatusDidChange:(EMMessage *)aMessage error:(EMError *)aError{}
群組
群組類型(4種)
EMGroupStylePrivateOnlyOwnerInvite 私有群組汤徽,創(chuàng)建完成后娩缰,只允許 Owner 邀請用戶加入
EMGroupStylePrivateMemberCanInvite 私有群組,創(chuàng)建完成后谒府,只允許 Owner 和群成員邀請用戶加入
EMGroupStylePublicJoinNeedApproval 公開群組拼坎,創(chuàng)建完成后,只允許 Owner 邀請用戶加入; 非群成員用戶需發(fā)送入群申請完疫,Owner 同意后才能入組
EMGroupStylePublicOpenJoin 公開群組演痒,創(chuàng)建完成后,允許非群組成員加入趋惨,不需要管理員同意
創(chuàng)建群組
EMError *error = nil;
EMGroupOptions *setting = [[EMGroupOptions alloc] init];
setting.maxUsersCount = 500;
setting.style = EMGroupStylePublicOpenJoin;// 創(chuàng)建不同類型的群組鸟顺,這里需要才傳入不同的類型
EMGroup *group = [[EMClient sharedClient].groupManager createGroupWithSubject:@"群組名稱" description:@"群組描述" invitees:@[@"6001",@"6002"] message:@"邀請您加入群組" setting:setting error:&error];
if(!error){
NSLog(@"創(chuàng)建成功 -- %@",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: 返回的成員列表,內(nèi)部的值是成員的環(huán)信id器虾。
// result.cursor: 返回的cursor讯嫂,如果要取下一頁的列表,需要將這個cursor當(dāng)參數(shù)傳入到獲取群組成員列表中兆沙。
}
// 異步
[[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(@"修改成功");
}
}];
// 改變?nèi)褐鳎ū仨毷侨褐鳎? EMError *error = nil;
[[EMClient sharedClient].groupManager updateGroupOwner:@"groupId" newOwner:@"newOwner" error:&error];
// 改變?nèi)褐鳎ó惒剑? [[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];
// 內(nèi)存中獲取所有群組欧芽,第一次從數(shù)據(jù)庫加載
NSArray *groupList = [[EMClient sharedClient].groupManager getJoinedGroups];
// 獲取和登錄者相關(guān)的群組
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];
// 對方邀請我加入某群時調(diào)用
- (void)groupInvitationDidReceive:(NSString *)aGroupId inviter:(NSString *)aInviter message:(NSString *)aMessage{}
// 對方同意我的加群邀請時調(diào)用
- (void)groupInvitationDidAccept:(EMGroup *)aGroup invitee:(NSString *)aInvitee{}
// 對方拒絕我的加群邀請時調(diào)用
- (void)groupInvitationDidDecline:(EMGroup *)aGroup invitee:(NSString *)aInvitee reason:(NSString *)aReason{}
// 自動加入群后回調(diào)(對方邀請)(EMOptions的isAutoAcceptGroupInvitation為YES)
- (void)didJoinGroup:(EMGroup *)aGroup inviter:(NSString *)aInviter message:(NSString *)aMessage{}
// 離開群組后回調(diào)
- (void)didLeaveGroup:(EMGroup *)aGroup reason:(EMGroupLeaveReason)aReason{}
// 收到入群請求后回調(diào)(群的類型是EMGroupStylePublicJoinNeedApproval)
- (void)joinGroupRequestDidReceive:(EMGroup *)aGroup user:(NSString *)aUsername reason:(NSString *)aReason{}
// 申請入群被拒后回調(diào)(群的類型是EMGroupStylePublicJoinNeedApproval)
- (void)joinGroupRequestDidDecline:(NSString *)aGroupId reason:(NSString *)aReason{}
// 申請入群被同意后回調(diào)(群的類型是EMGroupStylePublicJoinNeedApproval)
- (void)joinGroupRequestDidApprove:(EMGroup *)aGroup{}
// 群組列表更新后回調(diào)
- (void)groupListDidUpdate:(NSArray *)aGroupList{}
// 有成員被加入禁言列表后調(diào)用
- (void)groupMuteListDidUpdate:(EMGroup *)aGroup addedMutedMembers:(NSArray *)aMutedMembers muteExpire:(NSInteger)aMuteExpire{}
// 有成員被移除禁言列表后調(diào)用
- (void)groupMuteListDidUpdate:(EMGroup *)aGroup removedMutedMembers:(NSArray *)aMutedMembers{}
// 有成員被加入管理員列表后調(diào)用
- (void)groupAdminListDidUpdate:(EMGroup *)aGroup addedAdmin:(NSString *)aAdmin{}
// 有成員被移除管理員列表后調(diào)用
- (void)groupAdminListDidUpdate:(EMGroup *)aGroup removedAdmin:(NSString *)aAdmin{}
// 群組創(chuàng)建者更新后回調(diào)
- (void)groupOwnerDidUpdate:(EMGroup *)aGroup newOwner:(NSString *)aNewOwner oldOwner:(NSString *)aOldOwner{}
// 有用戶加入群組后回調(diào)
- (void)userDidJoinGroup:(EMGroup *)aGroup user:(NSString *)aUsername{}
// 有用戶離開群組后調(diào)用
- (void)userDidLeaveGroup:(EMGroup *)aGroup user:(NSString *)aUsername{}
// 群公告更新后調(diào)用
- (void)groupAnnouncementDidUpdate:(EMGroup *)aGroup announcement:(NSString *)aAnnouncement{}
// 有用戶上傳文件后調(diào)用
- (void)groupFileListDidUpdate:(EMGroup *)aGroup addedSharedFile:(EMGroupSharedFile *)aSharedFile{}
// 有用戶刪除群共享文件
- (void)groupFileListDidUpdate:(EMGroup *)aGroup removedSharedFile:(NSString *)aFileId{}
群主
// 作為群主 收到入群申請時調(diào)用
- (void)didReceiveJoinGroupApplication:(EMGroup *)aGroup applicant:(NSString *)aApplicant reason:(NSString *)aReason{}
聊天頁面
進入系統(tǒng)單聊頁面
EaseMessageViewController *chatController = [[EaseMessageViewController alloc] initWithConversationChatter:@"8001" conversationType:EMConversationTypeChat];
進入系統(tǒng)群聊頁面
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) {
}];
// 發(fā)送已讀回執(zhí)(需要開發(fā)者手動調(diào)用)
[[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(@"修改成功");
}
}];
// 以下相關(guān)方法都有同步異步執(zhí)行,就省略了
// 將某成員移除聊天室
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];
// 改變聊天室創(chuàng)建者時調(diào)用
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];
// 有用戶加入聊天室后調(diào)用
- (void)userDidJoinChatroom:(EMChatroom *)aChatroom user:(NSString *)aUsername{}
// 有用戶離開聊天室后調(diào)用
- (void)userDidLeaveChatroom:(EMChatroom *)aChatroom user:(NSString *)aUsername{}
// 被踢出聊天室后調(diào)用
- (void)didDismissFromChatroom:(EMChatroom *)aChatroom reason:(EMChatroomBeKickedReason)aReason{}
// 有成員被禁言時調(diào)用
- (void)chatroomMuteListDidUpdate:(EMChatroom *)aChatroom addedMutedMembers:(NSArray *)aMutes muteExpire:(NSInteger)aMuteExpire{}
// 有成員被移除禁言列表時調(diào)用
- (void)chatroomMuteListDidUpdate:(EMChatroom *)aChatroom removedMutedMembers:(NSArray *)aMutes{}
// 有成員被加入管理員列表時調(diào)用
- (void)chatroomAdminListDidUpdate:(EMChatroom *)aChatroom addedAdmin:(NSString *)aAdmin
// 有成員被移出管理員列表時調(diào)用
- (void)chatroomAdminListDidUpdate:(EMChatroom *)aChatroom removedAdmin:(NSString *)aAdmin
// 聊天室創(chuàng)建者更新時調(diào)用
- (void)chatroomOwnerDidUpdate:(EMChatroom *)aChatroom newOwner:(NSString *)aNewOwner oldOwner:(NSString *)aOldOwner{}
// 聊天室公告有更新時調(diào)用
- (void)chatroomAnnouncementDidUpdate:(EMChatroom *)aChatroom announcement:(NSString *)aAnnouncement{}
通話
// 1.option配置
EMCallOptions *options = [[EMClient sharedClient].callManager getCallOptions];
// 當(dāng)對方不在線時葛圃,是否給對方發(fā)送離線消息和推送千扔,并等待對方回應(yīng)
options.isSendPushIfOffline = NO;
// 設(shè)置視頻分辨率:自適應(yīng)分辨率、352 * 288库正、640 * 480曲楚、1280 * 720
options.videoResolution = EMCallVideoResolutionAdaptive;
// 最大視頻碼率,范圍 50 < videoKbps < 5000, 默認0, 0為自適應(yīng)褥符,建議設(shè)置為0
options.maxVideoKbps = 0;
// 最小視頻碼率
options.minVideoKbps = 0;
// 是否固定視頻分辨率龙誊,默認為NO
options.isFixedVideoResolution = NO;
[[EMClient sharedClient].callManager setCallOptions:options];
// 2.發(fā)起通話
[[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.結(jié)束通話
[[EMClient sharedClient].callManager endCall:@"sessionId" reason:EMCallEndReasonHangup];
/
EMCallEndReasonHangup = 0, 對方掛斷
EMCallEndReasonNoResponse, 對方?jīng)]有響應(yīng)
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];
// 暫停語音數(shù)據(jù)傳輸
[callSession pauseVideo];
// 恢復(fù)語音數(shù)據(jù)傳輸
[callSession resumeVideo];
// 暫停視圖數(shù)據(jù)傳輸
[callSession pauseVideo];
// 恢復(fù)視頻數(shù)據(jù)傳輸
[callSession resumeVideo];
// 是否使用前置攝像頭,false:后置
[callSession switchCameraPosition:true];
// 添加dele <EMCallManagerDelegate>
[[EMClient sharedClient].callManager addDelegate:self delegateQueue:nil];
// 移除dele
[[EMClient sharedClient]removeDelegate:self];
// 收到視頻請求后調(diào)用
- (void)callDidReceive:(EMCallSession *)aSession
// 統(tǒng)一通話后雙方都回調(diào)
- (void)callDidConnect:(EMCallSession *)aSession
// 對方同意通話后回調(diào)
- (void)callDidAccept:(EMCallSession *)aSession
// 通話結(jié)束(包括出現(xiàn)錯誤)雙方都回調(diào)
- (void)callDidEnd:(EMCallSession *)aSession reason:(EMCallEndReason)aReason error:(EMError *)aError{}
// 對方中斷或者繼續(xù)數(shù)據(jù)流傳輸時調(diào)用
- (void)callStateDidChange:(EMCallSession *)aSession type:(EMCallStreamingStatus)aType{}
// 網(wǎng)絡(luò)狀態(tài)不穩(wěn)定時調(diào)用
- (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];
}
多設(shè)備
// 獲取當(dāng)前帳號在其他設(shè)備上的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];
//發(fā)送消息
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];
// 多設(shè)備收到好友時調(diào)用
- (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];
}
// 多設(shè)備收到群組時調(diào)用
- (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];
}