简介:
环信是移动即时通讯能力的云计算PaaS( Platform as a Service, 平台服务)平台服务商
环信将基于移动互联网的即时通讯能力, 如单聊, 群聊, 语音, 图片, 位置, 实时音频, 实时视频等, 通过云端开放的 Rest API 和客户端 SDK 包的方式提供给开发者和企业. 让App内置聊天功能和以前网页中嵌入分享功能一样简单. 让移动开发者摆脱繁重的移动IM通讯底层开发, 极大限度地缩短产品开发周期, 极短的时间内让App拥有移动IM能力
- IOS SDK中有三个子文件夹: include, lib, resources
- lib 包含两个静态库libEaseMobClientSDK.a 和 libEaseMobClientSDKLite.a
1、libEaseMobClientSDKLite.a不包括实时语音功能
2、libEaseMobClientSDK.a包含所有功能
3、如果app中不需要实时语音功能,删掉libEaseMobClientSDK.a只使用libEaseMobClientSDKLite.a即可
- resources idk的bundle,包含旧版的数据库,消息提示音, idk配置文件.
- include 包含idk的头文件. 所有接口都在这个文件中
初始化SDK
- 1、引入头文件
#import <EaseMob.h>
- 2、初始化
// 参数 1 : APPkey 由应用名字#公司的ID构成
// 参数 2 : 如果使用
[[EaseMob sharedInstance]registerSDKWithAppKey:@"lutianyi#easemobsample" apnsCertName:@""];
// 进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[EaseMob sharedInstance]applicationDidEnterBackground:application];}
// 进入前台
- (void)applicationWillEnterForeground:(UIApplication *)application {
[[EaseMob sharedInstance]applicationWillEnterForeground:application];}
// 申请处理时间
- (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 代理方法
//设置代理
[[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) {
//环信里错误类 相当于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中对应的类型是EMMessage, EMMessage可以由多个符合<IEMMessageBody>协议的body组成,推荐使用一个body.
//发送消息
- (void)sendMessage{
EMChatText *text = [[EMChatText alloc]initWithText:@"收到请回复,OVER"];
EMTextMessageBody *textMessageBody = [[EMTextMessageBody alloc]initWithChatObject:text];
EMMessage *message = [[EMMessage alloc]initWithReceiver:self.chatter bodies:@[textMessageBody]];
// 发送这条消息
[[EaseMob sharedInstance].chatManager asyncSendMessage:message progress:nil prepare:^(EMMessage *message, EMError *error)
{
//准备发送
}
onQueue:dispatch_get_main_queue() completion:^(EMMessage *message, EMError *error)
{
// 发送成功
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)];
}