簡介:
環(huán)信是移動即時通訊能力的云計算PaaS( Platform as a Service, 平臺服務(wù))平臺服務(wù)商
環(huán)信將基于移動互聯(lián)網(wǎng)的即時通訊能力, 如單聊, 群聊, 語音, 圖片, 位置, 實時音頻, 實時視頻等, 通過云端開放的 Rest API 和客戶端 SDK 包的方式提供給開發(fā)者和企業(yè). 讓App內(nèi)置聊天功能和以前網(wǎng)頁中嵌入分享功能一樣簡單. 讓移動開發(fā)者擺脫繁重的移動IM通訊底層開發(fā), 極大限度地縮短產(chǎn)品開發(fā)周期, 極短的時間內(nèi)讓App擁有移動IM能力
- IOS SDK中有三個子文件夾: include, lib, resources
- lib 包含兩個靜態(tài)庫libEaseMobClientSDK.a 和 libEaseMobClientSDKLite.a
1、libEaseMobClientSDKLite.a不包括實時語音功能
2霎迫、libEaseMobClientSDK.a包含所有功能
3斋枢、如果app中不需要實時語音功能,刪掉libEaseMobClientSDK.a只使用libEaseMobClientSDKLite.a即可
- resources idk的bundle,包含舊版的數(shù)據(jù)庫,消息提示音, idk配置文件.
- include 包含idk的頭文件. 所有接口都在這個文件中
初始化SDK
- 1、引入頭文件
#import <EaseMob.h>
- 2知给、初始化
// 參數(shù) 1 : APPkey 由應(yīng)用名字#公司的ID構(gòu)成
// 參數(shù) 2 : 如果使用
[[EaseMob sharedInstance]registerSDKWithAppKey:@"lutianyi#easemobsample" apnsCertName:@""];
// 進(jìn)入后臺
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[EaseMob sharedInstance]applicationDidEnterBackground:application];}
// 進(jìn)入前臺
- (void)applicationWillEnterForeground:(UIApplication *)application {
[[EaseMob sharedInstance]applicationWillEnterForeground:application];}
// 申請?zhí)幚頃r間
- (void)applicationWillTerminate:(UIApplication *)application {
[[EaseMob sharedInstance] applicationWillTerminate:application];
}`
//注冊—異步的Block方式
- (IBAction)registerButtonAction:(id)sender {
[[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:self.userNameTF.text password:self.passWordTF.text withCompletion:^(NSString *username, NSString *password, EMError *error) {
if (error == nil) {
[self.delegate passValueUserName:self.userNameTF.text PassWord:self.passWordTF.text];
[self dismissViewControllerAnimated:YES completion:nil]; }
onQueue:dispatch_get_main_queue()];
}
// 登錄---異步的Block方法
- (IBAction)loginButtonAction:(id)sender {
[[EaseMob sharedInstance].chatManager asyncLoginWithUsername:self.userNameTF.text password:self.passWordTF.text completion:^(NSDictionary *loginInfo, EMError *error) {
if (error == nil) {
AppDelegate *app = [UIApplication sharedApplication].delegate;
[app createTabBar];
NSLog(@"登錄成功"); } }
onQueue:dispatch_get_main_queue()];}
// 添加好友 ---
- (IBAction)addRosterAction:(id)sender {
EMError *error = nil;
BOOL succeed = [[EaseMob sharedInstance].chatManager addBuddy:self.nameTF.text message:@"約嗎?" error:&error];
if (succeed && !error) {
[self.navigationController popToRootViewControllerAnimated:YES]; }}
#pragma mark ---EMChatManagerDelegate 代理方法
//設(shè)置代理
[[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:dispatch_get_main_queue()];
// 收到好友請求
- (void)didReceiveBuddyRequest:(NSString *)username message:(NSString *)message{
NSString *title = [NSString stringWithFormat:@"%@ 跪求添加您為好友", username];
UIAlertController *controller = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:(UIAlertControllerStyleAlert)];
UIAlertAction *actionAgree = [UIAlertAction actionWithTitle:@"行吧" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
//環(huán)信里錯誤類 相當(dāng)于NSError
EMError *error = nil;
BOOL succeed = [[EaseMob sharedInstance].chatManager acceptBuddyRequest:username error:&error];
if (succeed && !error) {
NSLog(@"接收成功"); } else
{ NSLog(@"接收失敗"); } }];
UIAlertAction *actionDisagree = [UIAlertAction actionWithTitle:@"丑拒" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
EMError *error = nil;
BOOL succeed = [[EaseMob sharedInstance].chatManager rejectBuddyRequest:username reason:@"呵呵呵" error:&error];
if (succeed && !error) {
NSLog(@"已拒絕成功"); }
else { NSLog(@"拒絕失敗"); } }];
[controller addAction:actionAgree];
[controller addAction:actionDisagree];
[self presentViewController:controller animated:YES completion:nil];}
- 消息: IM交互實體,在SDK中對應(yīng)的類型是EMMessage, EMMessage可以由多個符合<IEMMessageBody>協(xié)議的body組成,推薦使用一個body.
//發(fā)送消息
- (void)sendMessage{
EMChatText *text = [[EMChatText alloc]initWithText:@"收到請回復(fù),OVER"];
EMTextMessageBody *textMessageBody = [[EMTextMessageBody alloc]initWithChatObject:text];
EMMessage *message = [[EMMessage alloc]initWithReceiver:self.chatter bodies:@[textMessageBody]];
// 發(fā)送這條消息
[[EaseMob sharedInstance].chatManager asyncSendMessage:message progress:nil prepare:^(EMMessage *message, EMError *error)
{
//準(zhǔn)備發(fā)送
}
onQueue:dispatch_get_main_queue() completion:^(EMMessage *message, EMError *error)
{
// 發(fā)送成功
if (!error) {
[self reloadMessage]; } }
onQueue:dispatch_get_main_queue()];}
// 接收到消息
- (void)didReceiveMessage:(EMMessage *)message{
[self reloadMessage];
}
// 獲取某聊天對象的所有聊天信息
- (void)reloadMessage{
self.dataArray = [[EaseMob sharedInstance].chatManager conversationForChatter:self.chatter conversationType:(eConversationTypeChat)];
}