容联云通讯2---->开发指南

容联: IM+短信验证+语音验证码....功能
容联iOS开发指南
http://www.yuntongxun.com/api/im/iosWiki#point_box

1、iOS集成指南

1.1、导入sdk
开发者集成云通讯sdk,需要:
(1)先下载sdk,解压之后目录如图所示例:

容联云通讯2---->开发指南_第1张图片
Paste_Image.png

Yuntx_IMLib_V5.0.1r.a库是静态库,Manager文件夹是主调函数声明,Delegate文件夹是回调函数声明。

(2)然后导入sdk。将解压后的文件夹(Yuntx_IMLib_SDK)拖入您的工程中,并勾选上Destination,如图所示:

容联云通讯2---->开发指南_第2张图片
Paste_Image.png

点击finish,完成SDK添加,工程目录如下图所示:

容联云通讯2---->开发指南_第3张图片
Paste_Image.png

注意:由于iOS编译的特殊性,静态库中包含i386、x86_64、armv7、arm64平台,导致SDK的静态库(.a文件)会比较大,但您实际集成编译出ipa后,只会增加2MB左右。

1.2、设置工程属性
向Build Phases -> Link Binary With Libraries 中添加系统依赖库,操作步骤如下所示:

容联云通讯2---->开发指南_第4张图片
Paste_Image.png

按照上图,点击加号后,显示下图:在搜索框中输入需要的依赖库名称,如添加sqlite库,按图步骤,点击Add添加依赖库成功

容联云通讯2---->开发指南_第5张图片
Paste_Image.png

SDK需要添加系统依赖库如下:

libicucore.dylib 
libsqlite3.dylib 
libz.dylib
CoreTelephony.framework
MediaPlayer.framework
CFNetwork.framework 
SystemConfiguration.framework MobileCoreServices.framework 
AudioToolbox.framework 
CoreGraphics.framework 
AVFoundation.framework

添加完依赖库之后,第一步即完成,可以进行下一步了。

编译设置:

因为SDK采用的是C代码编写,所以需要应用设置混编设置:

1、如图选择stdlibc++模式编译

容联云通讯2---->开发指南_第6张图片
Paste_Image.png

2、在第一次调用的地方,如demo中,更改AppDelegate.m文件的后缀为.mm如果不设置这两项,编译时出现std::错误。

2、开发指南

2.1、请求回调和通知回调

SDK中和服务端的交互采取异步回调方法,有两种异步回调方法,一种是请求回调方法,一种是通知回调方法,详细介绍如下:

(1)请求回调方法:即由用户主动发出一个操作请求,请求的结果在回调中返回。具体说就是在调用sdk的方法时,sdk的方法参数中有回调方法,请求的结果直接在参数的回调方法中处理。例如发送消息的方法,包含获知消息是否发送成功的回调,此方法就是请求回调方法。

(2)通知回调方法:通知回调是指由云通讯服务端主动给客户端发送的信令或业务消息,如接收消息,就是由服务端通知应用来收消息,此方法就是通知回调方法。

2.2初始化、登陆及登出

✾ 在程序入口初始化SDK并设置代理

//设置回调代理
[ECDevice sharedInstance].delegate = [DeviceDelegateHelper sharedInstance];```

代理类示例:
DeviceDelegateHelper.h文件

@interface DeviceDelegateHelper : NSObject
/** *@brief 获取DeviceDelegateHelper单例句柄 /
+(DeviceDelegateHelper
)sharedInstance;
@end ```

DeviceDelegateHelper.m文件

@implementation DeviceDelegateHelper
+(DeviceDelegateHelper*)sharedInstance{
 
  static DeviceDelegateHelper *devicedelegatehelper; 
  static dispatch_once_t devicedelegatehelperonce; 
  dispatch_once(&devicedelegatehelperonce, ^{
     devicedelegatehelper = [[DeviceDelegateHelper alloc] init]; 
  }); 
  return devicedelegatehelper;
}

/** 
@brief 连接状态接口 
@discussion 监听与服务器的连接状态 V5.0版本接口 
@param state 连接的状态 
@param error 错误原因值 
*/
-(void)onConnectState:(ECConnectState)state failed:(ECError*)error{ 
    switch (state) { 
      case State_ConnectSuccess: 
        //连接成功 break; 
      case State_Connecting:
        //连接中; break; 
      case State_ConnectFailed:
        //与服务器断开连接break; 
      default: break; 
  }
}

/** 
@brief 个人信息版本号
 @param version服务器上的个人信息版本号
*/
-(void)onServicePersonVersion:(unsigned long long)version{
}

/** 
@brief 接收即时消息代理函数 
@param message 接收的消息
 */
-(void)onReceiveMessage:(ECMessage*)message{
}

/** 
@brief 离线消息数 
@param count 消息数 
*/
-(void) onOfflineMessageCount:(NSUInteger)count{
}

/** 
@brief 需要获取的消息数 
@return 消息数 -1:全部获取 0:不获取 
*/
-(NSInteger) onGetOfflineMessage{
}

/** 
@brief 接收离线消息代理函数 
@param message 接收的消息 
*/
-(void) onReceiveOfflineMessage:(ECMessage*)message{
}

/** 
@brief 离线消息接收是否完成 
@param isCompletion YES:拉取完成 NO:拉取未完成(拉取消息失败) 
*/
-(void) onReceiveOfflineCompletion:(BOOL)isCompletion{
}

/** 
@brief 客户端录音振幅代理函数 
@param amplitude 录音振幅 
*/
-(void)onRecordingAmplitude:(double) amplitude{
}

/** 
@brief 接收群组相关消息 
@discussion 参数要根据消息的类型,转成相关的消息类; 解散群组、收到邀请、申请加入、退出群组、有人加入、移除成员等消息
@param groupMsg 群组消息 
*/
-(void)onReceiveGroupNoticeMessage:(ECGroupNoticeMessage *)groupMsg{}
@end ```

###✾ 登录—具体代码如下:

//默认模式、只对AppKey、AppToken和userName鉴权
ECLoginInfo * loginInfo = [[ECLoginInfo alloc] init];
loginInfo.username = @"用户名";//用户登录app的用户id即可。
loginInfo.appKey = @"appid";
loginInfo.appToken = @"apptoken";
loginInfo.authType = LoginAuthType_NormalAuth;//默认方式登录
loginInfo.mode = LoginMode_InputPassword;
[[ECDevice sharedInstance] login:loginInfo completion:^(ECError *error){
if (error.errorCode == ECErrorType_NoError) {
//登录成功
}else{
//登录失败 }
}];

//密码模式、对AppKey、userName和userPassword鉴权
ECLoginInfo * loginInfo = [[ECLoginInfo alloc] init];
loginInfo.username = @"用户名";
loginInfo.appKey = @"appid";
loginInfo. userPassword= @"用户密码";
loginInfo.authType = LoginAuthType_PasswordAuth;//密码方式登录
loginInfo.mode = LoginMode_InputPassword;
[[ECDevice sharedInstance] login:loginInfo completion:^(ECError *error){
if (error.errorCode == ECErrorType_NoError) {
//登录成功 }
else
{ //登录失败 }
}];

###✾ 登出
退出当前账号,具体代码如下:

[[ECDevice sharedInstance] logout:^(ECError *error) {
//登出结果
}];

2.3、单聊
###✾ 发送文本\图片\附件—我们假设Tony给John发送文本消息,则代码如下:

//发送图片
ECImageMessageBody *messageBody = [[ECImageMessageBody alloc] initWithFile:@"图片文件本地绝对路径" displayName:@"文件名称"];

//发送文件
ECFileMessageBody *messageBody = [[ECFileMessageBody alloc] initWithFile:@"文件本地绝对路径" displayName:@"文件名称"];

//发送文本
ECTextMessageBody *messageBody = [[ECTextMessageBody alloc] initWithText:@"你好,欢迎来到云通讯"];

初始化消息, 并指定发送的内容

ECMessage *message = [[ECMessage alloc] initWithReceiver:@"John的账号Id" body:messageBody];

// 取本地时间
NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval tmp =[date timeIntervalSince1970]*1000;
message.timestamp = [NSString stringWithFormat:@"%lld", (long long)tmp];

[[ECDevice sharedInstance].messageManager sendMessage:message progress:nil completion:^(ECError *error, ECMessage *amessage) {
if (error.errorCode == ECErrorType_NoError) {
//发送成功
}else if(error.errorCode == ECErrorType_Have_Forbid || error.errorCode == ECErrorType_File_Have_Forbid) {
//您已被群组禁言
}else{
//发送失败
}
}];


###✾ 发送语音—我们假设Tony给John发送语音消息,则代码如下:

//开始录音
-(void)startRecord{
ECVoiceMessageBody * messageBody = [[ECVoiceMessageBody alloc] initWithFile:@"语音文件路径.arm" displayName:@"文件名.arm"];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[[ECDevice sharedInstance].messageManager startVoiceRecording:messageBody error:^(ECError *error, ECVoiceMessageBody messageBody) {
if (error.errorCode == ECErrorType_RecordTimeOut) {
//录音超时,立即发送;应用也可以选择不发送
ECMessage message = [[ECMessage alloc] initWithReceiver:@"John的账号Id" body:messageBody];
NSDate
date = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval tmp =[date timeIntervalSince1970]
1000;
message.timestamp = [NSString stringWithFormat:@"%lld", (long long)tmp];
[[ECDevice sharedInstance].messageManager sendMessage:message progress:nil completion:^(ECError *error, ECMessage *amessage) {
if (error.errorCode == ECErrorType_NoError) {
//发送成功
}else if(error.errorCode == ECErrorType_Have_Forbid || error.errorCode == ECErrorType_File_Have_Forbid){
//您已被群组禁言
}else{
//发送失败
}
}];
}
}];
}

//停止录音
-(void)stopRecord {
[[ECDevice sharedInstance].messageManager stopVoiceRecording:^(ECError *error, ECVoiceMessageBody messageBody) {
if (error.errorCode == ECErrorType_NoError) {
ECMessage message = [[ECMessage alloc] initWithReceiver:@"John的账号Id" body:messageBody]; NSDate date = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval tmp =[date timeIntervalSince1970]
1000;
message.timestamp = [NSString stringWithFormat:@"%lld", (long long)tmp];
[[ECDevice sharedInstance].messageManager sendMessage:message progress:nil completion:^(ECError *error, ECMessage *amessage) {
if (error.errorCode == ECErrorType_NoError) {
//发送成功
}else if(error.errorCode == ECErrorType_Have_Forbid || error.errorCode == ECErrorType_File_Have_Forbid){
//您已被群组禁言
}else{
//发送失败
}
}];
} else if (error.errorCode == ECErrorType_RecordTimeTooShort) {
//录音时间过短
}
}];
}



###✾ 接收消息—我们假设John收到Tony发送过来的消息,则代码如下:

/**
@该通知回调接口在代理类里面
@brief 接收即时消息代理函数
@param message 接收的消息
/
-(void)onReceiveMessage:(ECMessage
)message{
NSLog:(@"收到%@的消息,属于%@会话", message.from, message.sessionId);
switch(message.messageBody.messageBodyType){
case MessageBodyType_Text:{
ECTextMessageBody *msgBody = (ECTextMessageBody *)message.messageBody;
NSLog(@"收到的是文本消息------%@,msgBody.text");
break;
}
case MessageBodyType_Voice:{
ECVoiceMessageBody *msgBody = (ECVoiceMessageBody *)message.messageBody;
NSLog(@"音频文件remote路径------%@",msgBody. remotePath);
break;
}
case MessageBodyType_Video:{
ECVideoMessageBody *msgBody = (ECVideoMessageBody *)message.messageBody;
NSLog(@"视频文件remote路径------%@",msgBody. remotePath);
break;
}
case MessageBodyType_Image:{
ECImageMessageBody *msgBody = (ECImageMessageBody *)message.messageBody;
NSLog(@"图片文件remote路径------%@",msgBody. remotePath);
NSLog(@"缩略图片文件remote路径------%@",msgBody. thumbnailRemotePath);
break;
}
case MessageBodyType_File:{
ECFileMessageBody *msgBody = (ECFileMessageBody *)message.messageBody;
NSLog(@"文件remote路径------%@",msgBody. remotePath);
break;
}
default: break;
}
}

2.5、群组操作
###✾ 创建群组—我们假设Tony要创建一个名为"出彩中国人"具体代码如下:

ECGroup * newgroup = [[ECGroup alloc] init];
newgroup.name = @"出彩中国人";
newgroup.declared = @"欢迎来到容联云通讯";
[[ECDevice sharedInstance].messageManager createGroup:newgroup completion:^(ECError *error, ECGroup *group) {
if (error.errorCode == ECErrorType_NoError) {
NSLog(@"创建群组成功 群组ID:%@", group.groupId);
} else{
NSLog(@"创建群组失败 errorCode:%d\r\nerrorDescription:%@", (int)error.errorCode,error.errorDescription);
}
}];```

✾ 主动加入—只要知道群组id,就可以主动加入群组。

1、我们假设"出彩中国人"群组已经有Tony和John两位成员,现在Smith要加入"出彩中国人"群组,则代码如下:

[[ECDevice sharedInstance].messageManager joinGroup:@"出彩中国人群组ID" reason:@"我要参加出彩中国人" completion:^(ECError *error, NSString *groupId) {
       __strong __typeof(weakSelf)strongSelf = weakSelf; 
      if (error.errorCode==ECErrorType_Have_Joined) { 
            NSLog(@"您已经在群组%@", groupId); 
      }else if(error.errorCode==ECErrorType_NoError){ 
                if(strongSelf.applyGroup.mode == ECGroupPermMode_DefaultJoin) {  
                   NSLog(@"加入群组%@成功!", groupId);  
                }else{ 
                   NSLog(@"申请加入已发出,请等待群主同意请求"); 
           } }else{ 
               NSLog(@"加入群组失败 errorCode:%d\r\nerrorDescription:%@", (int)error.errorCode,error.errorDescription); 
              }
}];```
注意:如果群组"出彩中国人"是公开群组,则smith直接被允许加入。如果群组"出彩中国人"是私有群组,则smith加入需要群主Tony的的同意。Tony首先收到Smith加入的请求,代码如下:

/**
@该通知回调接口是代理类里面统一的关于群组的"群组通知回调接口"—onReceiveGroupNoticeMessage
@brief 接收群组相关消息
@discussion 参数要根据消息的类型,转成相关的消息类; 解散群组、收到邀请、申请加入、退出群组、有人加入、移除成员等消息
@param groupMsg 群组消息
*/
-(void)onReceiveGroupNoticeMessage:(ECGroupNoticeMessage *)groupMsg{
if (groupMsg.messageType == ECGroupMessageType_Propose) {
ECProposerMsg * message = (ECProposerMsg *)groupMsg;
NSString *declared = @"";
if (message.declared.length>0) {
declared = [NSString stringWithFormat:@",理由:%@",message.declared];
}

   NSLog(@"\"%@\" 昵称:\"%@\" 申请加入讨论组\"%@\"%@",message.proposer,message.nickName, message.groupId, declared); 
 }

}```

Tony同意Smith加入的请求,代码如下:

/** 
@brief 管理员验证用户申请加入群组 
@param groupId 申请加入的群组id 
@param memberId 申请加入的成员id 
@param type 是否同意 
@param completion 执行结果回调block 
*/
[[ECDevice sharedInstance].messageManager ackJoinGroupRequest:@"出彩中国人群组ID" member:@" Smith的账号Id" ackType:EAckType_Agree completion:^(ECError *error, NSString *gorupId, NSString *memberId) { 
    if (error.errorCode == ECErrorType_NoError || error.errorCode == ECErrorType_Have_Joined) {
         NSLog(@"加入群组"); 
    }else{ 
        NSLog(@"errorCode:%d\rerrorDescription:%@",(int)error.errorCode,error.errorDescription); 
    }
}]; 

Smith加入成功后,群组中成员Tony和John收到的通知回调方法代码如下:

/** 
@该通知回调接口是代理类里面统一的关于群组的"群组通知回调接口"—onReceiveGroupNoticeMessage 
@brief 接收群组相关消息
 @discussion 参数要根据消息的类型,转成相关的消息类; 解散群组、收到邀请、申请加入、退出群组、有人加入、移除成员等消息 
@param groupMsg 群组消息 
*/
-(void)onReceiveGroupNoticeMessage:(ECGroupNoticeMessage *)groupMsg{ 
    if (groupMsg.messageType == ECGroupMessageType_ReplyJoin) {
      ECReplyJoinGroupMsg *message = (ECReplyJoinGroupMsg *)groupMsg; 
      NSLog(@"讨论组\"%@\"%@\"%@\"的加入申请",message.groupId,message.confirm==2?@"同意":@"拒绝", message.member); 
    }
} ```

你可能感兴趣的:(容联云通讯2---->开发指南)