環(huán)信SDK的使用
在蘋果開發(fā)者網(wǎng)站進(jìn)行制作推送證書
制作推送證書時需根據(jù)開發(fā)環(huán)境不同申請所需的證書類型:
1)對于開發(fā)環(huán)境(sandbox)的推送證書,請選擇 Apple Push Notification service SSL (Sandbox)
2)對于生產(chǎn)環(huán)境(production)的推送證書,請選擇 Apple Push Notification service SSL (Production)
證書申請完之后下載并用鑰匙串打開導(dǎo)出為P12格式.
登錄環(huán)信管理后臺,創(chuàng)建應(yīng)用,將對應(yīng)的信息填寫完整后,上傳在蘋果開發(fā)者賬號中申請的推送證書
環(huán)信SDK的導(dǎo)入
1)手動導(dǎo)入:從環(huán)信官網(wǎng)下載HyphenateLite SDK 導(dǎo)入到工程目錄中,并添加所需的依賴庫;
2)自動導(dǎo)入:用cocopods導(dǎo)入,在工程中創(chuàng)建podfile文件,并pod ‘HyphenateLite_CN’;
環(huán)信SDK的基礎(chǔ)功能
配置聊天賬號
a.初始化SDK
第一步: 引入相關(guān)頭文件 #import <HyphenateLite/EMSDK.h>。
第二步: 在工程的 AppDelegate 中的以下方法中,調(diào)用 SDK 對應(yīng)方法。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//AppKey:注冊的AppKey,詳細(xì)見下面注釋。
//apnsCertName:推送證書名(不需要加后綴),詳細(xì)見下面注釋。
EMOptions *options = [EMOptions optionsWithAppkey:@"douser#istore"];
options.apnsCertName = @"istore_dev";
[[EMClient sharedClient] initializeSDKWithOptions:options];
return YES;
}
// APP進(jìn)入后臺
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[EMClient sharedClient] applicationDidEnterBackground:application];
}
// APP將要從后臺返回
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[[EMClient sharedClient] applicationWillEnterForeground:application];
}
b.注冊
EMError *error = [[EMClient sharedClient] registerWithUsername:@"8001" password:@"111111"];
if (error==nil) {
NSLog(@"注冊成功");
}
c.登錄
EMError *error = [[EMClient sharedClient] loginWithUsername:@"8001" password:@"111111"];
if (!error) {
NSLog(@"登錄成功");
}
d.自動登錄
BOOL isAutoLogin = [EMClient sharedClient].options.isAutoLogin;
if (!isAutoLogin) {
EMError *error = [[EMClient sharedClient] loginWithUsername:@"8001" password:@"111111"];
}
e.重連
/*!
* SDK連接服務(wù)器的狀態(tài)變化時會接收到該回調(diào)
*
* 有以下幾種情況,會引起該方法的調(diào)用:
* 1. 登錄成功后,手機(jī)無法上網(wǎng)時,會調(diào)用該回調(diào)
* 2. 登錄成功后,網(wǎng)絡(luò)狀態(tài)變化時,會調(diào)用該回調(diào)
*
* @param aConnectionState 當(dāng)前狀態(tài)
*/
- (void)didConnectionStateChanged:(EMConnectionState)aConnectionState;
f.退出登錄
EMError *error = [[EMClient sharedClient] logout:YES];
if (!error) {
NSLog(@"退出成功");
}
g.被動退出登錄
/*!
* 當(dāng)前登錄賬號在其它設(shè)備登錄時會接收到該回調(diào)
*/
- (void)didLoginFromOtherDevice;
/*!
* 當(dāng)前登錄賬號已經(jīng)被從服務(wù)器端刪除時會收到該回調(diào)
*/
- (void)didRemovedFromServer;
快速集成群聊功能
a.創(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);
}
b.初始化聊天界面(需導(dǎo)入EaseUI)
//群ID:@"groupId"
//聊天類型:EMConversationTypeGroupChat
EaseMessageViewController *chatController = [[EaseMessageViewController alloc] initWithConversationChatter:@"groupId" conversationType:EMConversationTypeGroupChat];
快速集成好友功能
a.添加好友
[[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(@"拒絕好友成功");
}
}];
b.刪除好友
// 刪除好友
[[EMClient sharedClient].contactManager deleteContact:@"8001"
completion:^(NSString *aUsername, EMError *aError) {
if (!aError) {
NSLog(@"刪除成功");
}
}];
c.獲取好友
//從服務(wù)器獲取所有的好友
[[EMClient sharedClient].contactManager getContactsFromServerWithCompletion:^(NSArray *aList, EMError *aError) {
if (!aError) {
NSLog(@"獲取成功");
}
}];
//從數(shù)據(jù)庫獲取所有的好友
NSArray *userlist = [[EMClient sharedClient].contactManager getContacts];
構(gòu)造消息
a.構(gòu)造文字消息
EMTextMessageBody *body = [[EMTextMessageBody alloc] initWithText:@"要發(fā)送的消息"];
NSString *from = [[EMClient sharedClient] currentUsername];
//生成Message
EMMessage *message = [[EMMessage alloc] initWithConversationID:@"6001" from:from to:@"6001" body:body ext:messageExt];
message.chatType = EMChatTypeChat;// 設(shè)置為單聊消息
//message.chatType = EMChatTypeGroupChat;// 設(shè)置為群聊消息
//message.chatType = EMChatTypeChatRoom;// 設(shè)置為聊天室消息
b.構(gòu)造圖片消息
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;// 設(shè)置為單聊消息
//message.chatType = EMChatTypeGroupChat;// 設(shè)置為群聊消息
//message.chatType = EMChatTypeChatRoom;// 設(shè)置為聊天室消息
c.構(gòu)造語音消息
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;// 設(shè)置為單聊消息
//message.chatType = EMChatTypeGroupChat;// 設(shè)置為群聊消息
//message.chatType = EMChatTypeChatRoom;// 設(shè)置為聊天室消息
d.構(gòu)造視屏消息
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;// 設(shè)置為單聊消息
//message.chatType = EMChatTypeGroupChat;// 設(shè)置為群聊消息
//message.chatType = EMChatTypeChatRoom;// 設(shè)置為聊天室消息
e.構(gòu)造文件消息
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;// 設(shè)置為單聊消息
//message.chatType = EMChatTypeGroupChat;// 設(shè)置為群聊消息
//message.chatType = EMChatTypeChatRoom;// 設(shè)置為聊天室消息
f.構(gòu)造透傳消息
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.chatType = EMChatTypeChat;// 設(shè)置為單聊消息
//message.chatType = EMChatTypeGroupChat;// 設(shè)置為群聊消息
//message.chatType = EMChatTypeChatRoom;// 設(shè)置為聊天室消息
會話
a.新建或者獲取一個會話
[[EMClient sharedClient].chatManager getConversation:@"8001" type:EMConversationTypeChat createIfNotExist:YES];
//EMConversationTypeChat 單聊會話
//EMConversationTypeGroupChat 群聊會話
//EMConversationTypeChatRoom 聊天室會話
b.刪除一個會話
[[EMClient sharedClient].chatManager deleteConversation:@"8001" deleteMessages:YES];
c.獲取會話列表
EMConversation *conversation = [[EMClient sharedClient].chatManager getConversation:@"8001" type:EMConversationTypeChat createIfNotExist:YES];
d.獲取會話未讀消息數(shù)
[EMConversation unreadMessagesCount];
聊天
a.發(fā)送消息
/*!
@property
@brief 發(fā)送消息
@discussion
異步方法
*/
- (void)sendMessage:(EMMessage *)aMessage
progress:(void (^)(int progress))aProgressBlock
completion:(void (^)(EMMessage *message, EMError *error))aCompletionBlock;
b.接受消息
//消息回調(diào):EMChatManagerDelegate
//移除消息回調(diào)
[[EMClient sharedClient].chatManager removeDelegate:self];
//注冊消息回調(diào)
[[EMClient sharedClient].chatManager addDelegate:self delegateQueue:nil];
/*!
@method
@brief 接收到一條及以上非cmd消息
*/
- (void)didReceiveMessages:(NSArray *)aMessages;
c.解析普通消息
// 收到消息的回調(diào),帶有附件類型的消息可以用 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(@"大圖的下載狀態(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;
}
}
}
d.解析透傳消息
- (void)didReceiveCmdMessages:(NSArray *)aCmdMessages{
for (EMMessage *message in aCmdMessages) {
EMCmdMessageBody *body = (EMCmdMessageBody *)message.body;
NSLog(@"收到的action是 -- %@",body.action);
}
}
e.消息已送達(dá)回執(zhí)
/*!
@method
@brief 接收到一條及以上已送達(dá)回執(zhí)
*/
- (void)didReceiveHasDeliveredAcks:(NSArray *)aMessages;
f.消息已讀回執(zhí)
發(fā)送已讀回執(zhí)
// 發(fā)送已讀回執(zhí)。在這里寫只是為了演示發(fā)送,在APP中具體在哪里發(fā)送需要開發(fā)者自己決定。
[[EMClient sharedClient].chatManager asyncSendReadAckForMessage:message];
接受已讀回執(zhí)
/*!
* 接收到一條及以上已讀回執(zhí)
*
* @param aMessages 消息列表<EMMessage>
*/
- (void)didReceiveHasReadAcks:(NSArray *)aMessages;
以上方法基本都是在官方文檔中可以找到,編寫此文章,只為了方便大家隨時可以打開簡書,了解具體方法的使用,喜歡的請點個贊,謝謝!!