第 1 步:引入相關頭文件 #import 「EMSDK.h」。ios
第 2 步:在工程的 AppDelegate 中的如下方法中,調用 SDK 對應方法。git
- (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]; }
調用的 SDK 接口參數解釋以下:數據庫
環信爲 IM 部分提供了 APNS 推送功能,若是您要使用,請跳轉到APNS離線推送。服務器
註冊模式分兩種,開放註冊和受權註冊。網絡
EMError *error = [[EMClient sharedClient] registerWithUsername:@"8001" password:@"111111"]; if (error==nil) { NSLog(@"註冊成功"); }
登陸:調用 SDK 的登陸接口進行的操做。app
EMError *error = [[EMClient sharedClient] loginWithUsername:@"8001" password:@"111111"]; if (!error) { NSLog(@"登陸成功"); }
自動登陸:即首次登陸成功後,不須要再次調用登陸方法,在下次 APP 啓動時,SDK 會自動爲您登陸。而且若是您自動登陸失敗,也能夠讀取到以前的會話信息。異步
SDK 中自動登陸屬性默認是關閉的,須要您在登陸成功後設置,以便您在下次 APP 啓動時不須要再次調用環信登陸,而且能在沒有網的狀況下獲得會話列表。async
EMError *error = [[EMClient sharedClient] loginWithUsername:@"8001" password:@"111111"]; if (!error) { [[EMClient sharedClient].options setIsAutoLogin:YES]; }
自動登陸在如下幾種狀況下會被取消:ide
因此,在您調用登陸方法前,應該先判斷是否設置了自動登陸,若是設置了,則不須要您再調用。測試
BOOL isAutoLogin = [EMClient sharedClient].options.isAutoLogin; if (!isAutoLogin) { EMError *error = [[EMClient sharedClient] loginWithUsername:@"8001" password:@"111111"]; }
SDK 中,若是發生自動登陸,會有如下回調:
/*!
* 自動登陸返回結果
*
* @param aError 錯誤信息
*/
- (void)didAutoLoginWithError:(EMError *)aError //添加回調監聽代理: [[EMClient sharedClient] addDelegate:self delegateQueue:nil];
當掉線時,iOS SDK 會自動重連,只須要監聽重連相關的回調,無需進行任何操做。
/*!
* SDK鏈接服務器的狀態變化時會接收到該回調
*
* 有如下幾種狀況,會引發該方法的調用:
* 1. 登陸成功後,手機沒法上網時,會調用該回調
* 2. 登陸成功後,網絡狀態變化時,會調用該回調
*
* @param aConnectionState 當前狀態
*/
- (void)didConnectionStateChanged:(EMConnectionState)aConnectionState;
退出登陸分兩種類型:主動退出登陸和被動退出登陸。
logout:YES:是否解除 device token 的綁定,在被動退出時 SDK 內部處理,不須要調用退出方法。
EMError *error = [[EMClient sharedClient] logout:YES]; if (!error) { NSLog(@"退出成功"); }
使用回調方法監聽被動退出登陸。
/*! * 當前登陸帳號在其它設備登陸時會接收到該回調 */ - (void)didLoginFromOtherDevice; /*! * 當前登陸帳號已經被從服務器端刪除時會收到該回調 */ - (void)didRemovedFromServer;
消息:IM 交互實體,在 SDK 中對應的類型是 EMMessage。EMMessage 由 EMMessageBody 組成。
EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:@"要發送的消息"]; NSString *from = [[EMClient sharedClient] currentUsername]; //生成Message EMMessage *message = [[EMMessage alloc] initWithConversationID:@"6001" from:from to:@"6001" body:body ext:messageExt]; message.chatType = EMChatTypeChat;// 設置爲單聊消息 //message.chatType = EMChatTypeGroupChat;// 設置爲羣聊消息 //message.chatType = EMChatTypeChatRoom;// 設置爲聊天室消息
EMImageMessageBody *body = [[EMImageMessageBody alloc] initWithData:data displayName:@"image.png"]; NSString *from = [[EMClient sharedClient] currentUsername]; //生成Message EMMessage *message = [[EMMessage alloc] initWithConversationID:@"6001" from:from to:@"6001" body:body ext:messageExt]; message.chatType = EMChatTypeChat;// 設置爲單聊消息 //message.chatType = EMChatTypeGroupChat;// 設置爲羣聊消息 //message.chatType = EMChatTypeChatRoom;// 設置爲聊天室消息
EMLocationMessageBody *body = [[EMLocationMessageBody alloc] initWithLatitude:39 longitude:116 address:@"地址"]; NSString *from = [[EMClient sharedClient] currentUsername]; // 生成message EMMessage *message = [[EMMessage alloc] initWithConversationID:@"6001" from:from to:@"6001" body:body ext:messageExt]; message.chatType = EMChatTypeChat;// 設置爲單聊消息 //message.chatType = EMChatTypeGroupChat;// 設置爲羣聊消息 //message.chatType = EMChatTypeChatRoom;// 設置爲聊天室消息
EMVoiceMessageBody *body = [[EMVoiceMessageBody alloc] initWithLocalPath:@"audioPath" displayName:@"audio"]; body.duration = duration; NSString *from = [[EMClient sharedClient] currentUsername]; // 生成message EMMessage *message = [[EMMessage alloc] initWithConversationID:@"6001" from:from to:@"6001" body:body ext:messageExt]; message.chatType = EMChatTypeChat;// 設置爲單聊消息 //message.chatType = EMChatTypeGroupChat;// 設置爲羣聊消息 //message.chatType = EMChatTypeChatRoom;// 設置爲聊天室消息
EMVideoMessageBody *body = [[EMVideoMessageBody alloc] initWithLocalPath:@"videoPath" displayName:@"video.mp4"]; NSString *from = [[EMClient sharedClient] currentUsername]; // 生成message EMMessage *message = [[EMMessage alloc] initWithConversationID:@"6001" from:from to:@"6001" body:body ext:messageExt]; message.chatType = EMChatTypeChat;// 設置爲單聊消息 //message.chatType = EMChatTypeGroupChat;// 設置爲羣聊消息 //message.chatType = EMChatTypeChatRoom;// 設置爲聊天室消息
EMFileMessageBody *body = [[EMFileMessageBody alloc] initWithLocalPath:@"filePath" displayName:@"file"]; NSString *from = [[EMClient sharedClient] currentUsername]; // 生成message EMMessage *message = [[EMMessage alloc] initWithConversationID:@"6001" from:from to:@"6001" body:body ext:messageExt]; message.chatType = EMChatTypeChat;// 設置爲單聊消息 //message.chatType = EMChatTypeGroupChat;// 設置爲羣聊消息 //message.chatType = EMChatTypeChatRoom;// 設置爲聊天室消息
SDK 提供的一種特殊類型的消息,即 CMD,不會存 db,也不會走 APNS 推送,相似一種指令型的消息。好比您的服務器要通知客戶端作某些操做,您能夠服務器和客戶端提早約定好某個字段,當客戶端收到約定好的字段時,執行某種特殊操做。
EMCmdMessageBody *body = [[EMCmdMessageBody alloc] initWithAction:action]; NSString *from = [[EMClient sharedClient] currentUsername]; // 生成message EMMessage *message = [[EMMessage alloc] initWithConversationID:@"6001" from:from to:@"6001" body:body ext:messageExt]; message.messageType = eMessageTypeChat; // 設置爲單聊消息 //message.messageType = eConversationTypeGroupChat;// 設置爲羣聊消息 //message.messageType = eConversationTypeChatRoom;// 設置爲聊天室消息
當 SDK 提供的消息類型不知足需求時,開發者能夠經過擴展自 SDK 提供的文本、語音、圖片、位置等消息類型,從而生成本身須要的消息類型。
這裏是擴展自文本消息,若是這個自定義的消息須要用到語音或者圖片等,能夠擴展自語音、圖片消息,亦或是位置消息。
// 以單聊消息舉例
EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:@"要發送的消息"]; NSString *from = [[EMClient sharedClient] currentUsername]; //生成Message EMMessage *message = [[EMMessage alloc] initWithConversationID:@"6001" from:from to:@"6001" body:body ext:messageExt]; message.chatType = EMChatTypeChat;// 設置爲單聊消息 //message.chatType = EMChatTypeGroupChat;// 設置爲羣聊消息 //message.chatType = EMChatTypeChatRoom;// 設置爲聊天室消息 message.ext = @{@"key":@"value"}; // 擴展消息部分
EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:@"要發送的消息"]; NSString *from = [[EMClient sharedClient] currentUsername]; //生成Message EMMessage *message = [[EMMessage alloc] initWithConversationID:@"6001" from:from to:@"6001" body:body ext:messageExt]; message.chatType = EMChatTypeChat;// 設置爲單聊消息 //message.chatType = EMChatTypeGroupChat;// 設置爲羣聊消息 //message.chatType = EMChatTypeChatRoom;// 設置爲聊天室消息 [[EMClient sharedClient].chatManager importMessages:@[message]];
/*!
* 更新消息到 DB
*
* @param aMessage 消息
*
* @result 是否成功
*/
- (BOOL)updateMessage:(EMMessage *)aMessage; //調用:[[EMClient sharedClient].chatManager updateMessage:aMessage];
會話:操做聊天消息 EMMessage 的容器,在 SDK 中對應的類型是 EMConversation。
根據 conversationId 建立一個 conversation。
[[EMClient sharedClient].chatManager getConversation:@"8001" type:EMConversationTypeChat createIfNotExist:YES]; //EMConversationTypeChat 單聊會話 //EMConversationTypeGroupChat 羣聊會話 //EMConversationTypeChatRoom 聊天室會話
[[EMClient sharedClient].chatManager deleteConversation:@"8001" deleteMessages:YES];
[[EMClient sharedClient].chatManager deleteConversations:@[@"8001",@"8002"] deleteMessages:YES];
SDK中提供了三種獲取會會話列表的方法。
EMConversation *conversation = [[EMClient sharedClient].chatManager getConversation:@"8001" type:EMConversationTypeChat createIfNotExist:YES];
NSArray *conversations = [[EMClient sharedClient].chatManager getAllConversations];
NSArray *conversations = [[EMClient sharedClient].chatManager loadAllConversationsFromDB];
[EMConversation unreadMessagesCount];
能夠經過關鍵字、消息類型、開始結束時間檢索某個會話中的消息。
/*!
* 從數據庫獲取指定類型的消息,取到的消息按時間排序,若是參考的時間戳爲負數,則從最新消息向前取,若是 aLimit 是負數,則獲取全部符合條件的消息
*
* @param aType 消息類型
* @param aTimestamp 參考時間戳
* @param aLimit 獲取的條數
* @param aSender 消息發送方,若是爲空則忽略
* @param aDirection 消息搜索方向
*
* @result 消息列表<EMMessage>
*/
- (NSArray *)loadMoreMessagesWithType:(EMMessageBodyType)aType before:(long long)aTimestamp limit:(int)aLimit from:(NSString*)aSender direction:(EMMessageSearchDirection)aDirection; /*! * 從數據庫獲取包含指定內容的消息,取到的消息按時間排序,若是參考的時間戳爲負數,則從最新消息向前取,若是 aLimit 是負數,則獲取全部符合條件的消息 * * @param aKeywords 搜索關鍵字,若是爲空則忽略 * @param aTimestamp 參考時間戳 * @param aLimit 獲取的條數 * @param aSender 消息發送方,若是爲空則忽略 * @param aDirection 消息搜索方向 * * @result 消息列表<EMMessage> */ - (NSArray *)loadMoreMessagesContain:(NSString*)aKeywords before:(long long)aTimestamp limit:(int)aLimit from:(NSString*)aSender direction:(EMMessageSearchDirection)aDirection; /*! * 從數據庫獲取指定時間段內的消息,取到的消息按時間排序,爲了防止佔用太多內存,用戶應當制定加載消息的最大數 * * @param aStartTimestamp 毫秒級開始時間 * @param aEndTimestamp 結束時間 * @param aMaxCount 加載消息最大數 * * @result 消息列表<EMMessage> * */ - (NSArray *)loadMoreMessagesFrom:(long long)aStartTimestamp to:(long long)aEndTimestamp maxCount:(int)aMaxCount;
登陸成功以後才能進行聊天操做。發消息時,單聊和羣聊調用的是統一接口,區別只是要設置下 message.chatType。
/*!
@property
@brief 發送消息
@discussion
異步方法
*/
- (void)asyncSendMessage:(EMMessage *)aMessage progress:(void (^)(int progress))aProgress completion:(void (^)(EMMessage *message, EMError *error))aProgressCompletion; //調用:[[EMClient sharedClient].chatManager asyncSendMessage:message progress:nil completion:^(EMMessage *aMessage, EMError *aError) {}];
註冊消息回調
//消息回調:EMChatManagerChatDelegate
//註冊消息回調 [[EMClient sharedClient].chatManager addDelegate:self delegateQueue:nil]; //移除消息回調 [[EMClient sharedClient].chatManager removeDelegate:self];
在線普通消息會走如下回調:
/*!
@method
@brief 接收到一條及以上非cmd消息
*/
- (void)didReceiveMessages:(NSArray *)aMessages;
透傳(cmd)在線消息會走如下回調:
/*!
@method
@brief 接收到一條及以上cmd消息
*/
- (void)didReceiveCmdMessages:(NSArray *)aCmdMessages;
// 收到消息的回調,帶有附件類型的消息能夠用 SDK 提供的下載附件方法下載(後面會講到)
- (void)didReceiveMessages:(NSArray *)aMessages { for (EMMessage *message in aMessages) { 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; } } }
- (void)didReceiveCmdMessages:(NSArray *)aCmdMessages{ for (EMMessage *message in aCmdMessages) { EMCmdMessageBody *body = (EMCmdMessageBody *)message.body; NSLog(@"收到的action是 -- %@",body.action); } }
- (void)didReceiveCmdMessages:(NSArray *)aCmdMessages{ for (EMMessage *message in aCmdMessages) { // cmd消息中的擴展屬性 NSDictionary *ext = message.ext; NSLog(@"cmd消息中的擴展屬性是 -- %@",ext) } } // 收到消息回調 - (void)didReceiveMessages:(NSArray *)aMessages{ for (EMMessage *message in aMessages) { // 消息中的擴展屬性 NSDictionary *ext = message.ext; NSLog(@"消息中的擴展屬性是 -- %@",ext); } }
SDK 接收到消息後,會默認下載:圖片消息的縮略圖,語音消息的語音,視頻消息的視頻第一幀。
請先判斷你要下載附件沒有下載成功以後,在調用如下下載方法,不然SDK下載方法會再次從服務器上獲取附件。
[[EMClient sharedClient].chatManager asyncDownloadMessageThumbnail:message progress:nil completion:^(EMMessage *message, EMError *error) { if (!error) { NSLog(@"下載成功,下載後的message是 -- %@",aMessage); } }];
[[EMClient sharedClient].chatManager asyncDownloadMessageAttachments:message progress:nil completion:^(EMMessage *message, EMError *error) { if (!error) { NSLog(@"下載成功,下載後的message是 -- %@",aMessage); } }];
SDK提供了已送達回執,當對方收到您的消息後,您會收到如下回調。
/*!
@method
@brief 接收到一條及以上已送達回執
*/
- (void)didReceiveHasDeliveredAcks:(NSArray *)aMessages;
已讀回執須要開發者主動調用的。當用戶讀取消息後,由開發者主動調用方法。
// 發送已讀回執。在這裏寫只是爲了演示發送,在APP中具體在哪裏發送須要開發者本身決定。
[[EMClient sharedClient].chatManager asyncSendReadAckForMessage:message];
/*! * 接收到一條及以上已讀回執 * * @param aMessages 消息列表<EMMessage> */ - (void)didReceiveHasReadAcks:(NSArray *)aMessages;
注:環信不是好友也能夠聊天,不推薦使用環信的好友機制。若是你有本身的服務器或好友關係,請本身維護好友關係。
獲取好友列表,環信提供了兩種方法。
EMError *error = nil; NSArray *userlist = [[EMClient sharedClient].contactManager getContactsFromServerWithError:&error]; if (!error) { NSLog(@"獲取成功 -- %@",buddyList); }
NSArray *userlist = [[EMClient sharedClient].contactManager getContactsFromDB];
環信 iOS SDK 提供了添加好友的方法。
注:若是您已經發過,而且對方沒有處理,您將不能再次發送。
EMError *error = [[EMClient sharedClient].contactManager addContact:@"6001" message:@"我想加您爲好友"]; if (!error) { NSLog(@"添加成功"); }
當您收到好友請求,若是您沒有處理,請本身保存數據,新協議下不會每次都發送。
//註冊好友回調
[[EMClient sharedClient].contactManager addDelegate:self delegateQueue:nil]; //移除好友回調 [[EMClient sharedClient].contactManager removeDelegate:self];
監聽回調
/*!
* 用戶A發送加用戶B爲好友的申請,用戶B會收到這個回調
*
* @param aUsername 用戶名
* @param aMessage 附屬信息
*/
- (void)didReceiveFriendInvitationFromUsername:(NSString *)aUsername message:(NSString *)aMessage;
EMError *error = [[EMClient sharedClient].contactManager acceptInvitationForUsername:@"8001"]; if (!error) { NSLog(@"發送贊成成功"); }
EMError *error = [[EMClient sharedClient].contactManager declineInvitationForUsername:@"8001"]; if (!error) { NSLog(@"發送拒絕成功"); }
監聽回調
/*!
@method
@brief 用戶A發送加用戶B爲好友的申請,用戶B贊成後,用戶A會收到這個回調
*/
- (void)didReceiveAgreedFromUsername:(NSString *)aUsername; /*! @method @brief 用戶A發送加用戶B爲好友的申請,用戶B拒絕後,用戶A會收到這個回調 */ - (void)didReceiveDeclinedFromUsername:(NSString *)aUsername;
// 刪除好友
EMError *error = [[EMClient sharedClient].contactManager deleteContact:@"6001"]; if (!error) { NSLog(@"刪除成功"); }
環信的黑名單體系是獨立的,與好友無任何關係。也就是說,您能夠將任何人加入黑名單,不論他是否與您是好友關係。同時,若是您將好友好友加入黑名單,則他仍然是您的好友,只不過同時也在黑名單中。
查詢黑名單列表,環信提供了兩種方法。
EMError *error = nil; NSArray *blacklist = [[EMClient sharedClient].contactManager getBlackListFromServerWithError:&error]; if (!error) { NSLog(@"獲取成功 -- %@",blockedList); }
NSArray *blockList = [[EMClient sharedClient].contactManager getBlackListFromDB];
接口調用
// 將6001加入黑名單
EMError *error = [[EMClient sharedClient].contactManager addUserToBlackList:@"6001" relationshipBoth:YES]; if (!error) { NSLog(@"發送成功"); }
接口調用
// 將6001移除黑名單 EMError *error = [[EMClient sharedClient].contactManager removeUserFromBlackList:@"6001"]; if (!error) { NSLog(@"發送成功"); }
羣組分爲四種類型。
/*!
@enum
@brief 羣組類型
@constant EMGroupStylePrivateOnlyOwnerInvite 私有羣組,建立完成後,只容許 Owner 邀請用戶加入
@constant EMGroupStylePrivateMemberCanInvite 私有羣組,建立完成後,只容許 Owner 和羣成員邀請用戶加入
@constant EMGroupStylePublicJoinNeedApproval 公開羣組,建立完成後,只容許 Owner 邀請用戶加入; 非羣成員用戶需發送入羣申請,Owner 贊成後才能入組
@constant EMGroupStylePublicOpenJoin 公開羣組,建立完成後,容許非羣組成員加入,不須要管理員贊成
@discussion
eGroupStyle+Private:私有羣組,只容許羣組成員邀請人進入
eGroupStyle+Public: 公有羣組,容許非羣組成員加入
*/
typedef NS_ENUM(NSInteger, EMGroupStyle){ EMGroupStylePrivateOnlyOwnerInvite = 0, EMGroupStylePrivateMemberCanInvite, EMGroupStylePublicJoinNeedApproval, 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); }
/*!
@method
@brief 獲取羣組信息
@param aGroupId 羣組ID
@param aIncludeMembersList 是否獲取成員列表
@param pError 錯誤信息
@return 羣組
@discussion
同步方法,會阻塞當前線程
*/
- (EMGroup *)fetchGroupInfo:(NSString *)aGroupId includeMembersList:(BOOL)aIncludeMembersList error:(EMError **)pError; //調用: //EMError *error = nil; //EMGroup *group = [[EMClient sharedClient].groupManager fetchGroupInfo:@"groupId" includeMembersList:YES error:&error];
羣組分4種類型,目前 SDK 不支持自主選擇是否進羣。咱們將針對每種類型講解加入羣組要進行的操做。
註冊羣組回調:
//EMChatManagerDelegate
//註冊羣組回調 [[EMClient sharedClient].groupManager addDelegate:self delegateQueue:nil]; //移除羣組回調 [[EMClient sharedClient].groupManager removeDelegate:self];
被添加的人會收到回調:
/*!
@method
@brief 用戶B設置了自動贊成,用戶A邀請用戶B入羣,SDK 內部進行贊成操做以後,用戶B接收到該回調
*/
- (void)didJoinedGroup:(EMGroup *)aGroup inviter:(NSString *)aInviter message:(NSString *)aMessage;
加人接口以下:
EMError *error = nil; [[EMClient sharedClient].groupManager addOccupants:@[@"user1"] toGroup:@"groupId" welcomeMessage:@"message" error:&error];
// 申請加入須要審覈的公開羣組
EMError *error = nil; [[EMClient sharedClient].groupManager applyJoinPublicGroup:@"groupId" message:@"" error:nil];
只有 Owner 有權限處理進羣申請。
1. 收到進羣申請。
/*!
@method
@brief 用戶A向羣組G發送入羣申請,羣組G的羣主O會接收到該回調
*/
- (void)didReceiveJoinGroupApplication:(EMGroup *)aGroup applicant:(NSString *)aApplicant reason:(NSString *)aReason;
2. 贊成進羣申請。
/*!
@method
@brief 贊成加入羣組的申請
@param aGroupId 所申請的羣組 ID
@param aGroupname 申請的羣組名稱
@param aUsername 申請人的 username
@discussion
須要 Owner 權限
同步方法,會阻塞當前線程
*/
- (EMError *)acceptJoinApplication:(NSString *)aGroupId groupname:(NSString *)aGroupname applicant:(NSString *)aUsername; //調用: //EMError *error = [[EMClient sharedClient].groupManager acceptJoinApplication:@"groupId" groupname:@"subject" applicant:@"user1"];
3. 拒絕加羣申請。
EMError *error = [[EMClient sharedClient].groupManager declineJoinApplication:@"groupId" groupname:@"subject" applicant:@"user1" reason:@"拒絕的緣由"];
EMError *error = nil; [[EMClient sharedClient].groupManager joinPublicGroup:@"1410329312753" error:&error];
羣主(Owner)不支持退羣操做,只能解散羣。
退出羣組分爲主動退羣和被動退羣。被動退羣即爲被 Owner 踢出羣組。
EMError *error = nil; [[EMClient sharedClient].groupManager leaveGroup:@"1410329312753" error:&error];
會經過如下回調通知被踢者。
/*!
@method
@brief 接收到離開羣組,羣組被銷燬或者被從羣中移除
*/
- (void)didReceiveLeavedGroup:(EMGroup *)aGroup reason:(EMGroupLeaveReason)aReason;
解散羣組須要 Owner 權限。
EMError *error = nil; [[EMClient sharedClient].groupManager destroyGroup:@"groupId" error:&error]; if (!error) { NSLog(@"解散成功"); }
只有 Owner 有權限修改。
EMError *error = nil; // 修改羣名稱 [[EMClient sharedClient].groupManager changeGroupSubject:@"要修改的名稱" forGroup:@"1410329312753" error:&error]; if (!error) { NSLog(@"修改爲功"); }
不推薦使用 ,只有 Owner 有權限操做。
EMError *error = nil; // 修改羣描述 EMGroup* group = [[EMClient sharedClient].groupManager changeDescription:@"修改的羣描述" forGroup:@"1410329312753" error:&error]; if (!error) { NSLog(@"修改爲功"); }
只有 Owner 權限才能調用。
/*!
@method
@brief 將羣成員移出羣組
@param aOccupants 要請出羣組的人的用戶名列表
@param aGroupId 羣組ID
@param pError 錯誤信息
@result 返回羣組對象
@discussion
此操做須要 Owner 權限
同步方法,會阻塞當前線程
*/
- (EMGroup *)removeOccupants:(NSArray *)aOccupants fromGroup:(NSString *)aGroupId error:(EMError **)pError; //調用: //EMError *error = nil; //[[EMClient sharedClient].groupManager removeOccupants:@[@"user1"] fromGroup:@"1410329312753" error:&error];
只有 Owner 權限才能調用該接口,而且只有 Owner 權限的才能查看羣黑名單。
能夠將羣成員和非羣成員的人加入羣黑名單。
/*!
@method
@brief 將某些人加入羣組黑名單
@param aOccupants 要加入黑名單的用戶名列表
@param aGroupId 羣組ID
@param pError 錯誤信息
@result 返回羣組對象
@discussion
此操做須要 Owner 權限。被加入黑名單的人,不會再被容許進入羣組。
同步方法,會阻塞當前線程
*/
- (EMGroup *)blockOccupants:(NSArray *)aOccupants fromGroup:(NSString *)aGroupId error:(EMError **)pError; //調用: //EMError *error = nil; //EMGroup *group = [[EMClient sharedClient].groupManager blockOccupants:@[@"user1"] fromGroup:@"1410329312753" error:&error];
只有 Owner 權限才能調用該接口,而且只有 Owner 權限的才能查看羣黑名單。
從羣黑名單移除出去,該用戶已經不在羣組裏了,須要從新加入羣組。
/*!
@method
@brief 將某些人從羣組黑名單中解除
@param aOccupants 要從黑名單中移除的用戶名列表
@param aGroupId 羣組ID
@param pError 錯誤信息
@result 返回羣組對象
@discussion
此操做須要 Owner 權限。從黑名單中移除後再也不是羣組成員,須要從新加入。
同步方法,會阻塞當前線程
*/
- (EMGroup *)unblockOccupants:(NSArray *)aOccupants forGroup:(NSString *)aGroupId error:(EMError **)pError; //調用: //EMError *error = nil; //EMGroup *group = [[EMClient sharedClient].groupManager unblockOccupants:@[@"user1"] forGroup:@"1410329312753" error:&error];
不容許 Owner 權限的調用。
/*!
@method
@brief 屏蔽/取消屏蔽羣組推送
@param aGroupId 羣組ID
@param aIgnore 是否屏蔽
@result 錯誤信息
@discussion
同步方法,會阻塞當前線程
*/
- (EMError *)ignoreGroupPush:(NSString *)aGroupId ignore:(BOOL)aIgnore; //調用: //EMError *error = [[EMClient sharedClient].groupManager ignoreGroupPush:@"1410329312753" ignore:YES];
查看全部當前登陸帳號所在羣組,包括建立的和加入的羣組,提供了三種方法。
1.從服務器獲取與我相關的羣組列表
EMError *error = nil; NSArray *myGroups = [[EMClient sharedClient].groupManager getMyGroupsFromServerWithError:&error]; if (!error) { NSLog(@"獲取成功 -- %@",myGroups); }
2. 獲取數據庫中全部的羣組
NSArray *groupList = [[EMClient sharedClient].groupManager loadAllMyGroupsFromDB];
3. 取內存中的值
從內存中獲取全部羣組。
NSArray *groupList = [[EMClient sharedClient].groupManager getAllGroups];
獲取指定範圍內的公開羣。
EMError *error = nil; EMCursorResult *result = [[EMClient sharedClient].groupManager getPublicGroupsFromServerWithCursor:nil pageSize:50 error:&error]; if (!error) { NSLog(@"獲取成功 -- %@",result); }