初始化及登录

1、初始化SDK

                
 在程序入口初始化SDK并设置代理 - 代理类设置最好放在didFinishLaunchingWithOptions中,代码示例如下:             
                        
  • - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  •         [ECDevice sharedInstance].delegate = [DeviceDelegateHelper sharedInstance];
  •         return YES;}                    
 说明:SDK中不包含代理类DeviceDelegateHelper,需要开发者自己实现。
                          

2、代理类示例

2.1 DeviceDelegateHelper.h文件                      

  • //代理类.h文件如下   
  • //创建单例
  • import <Foundation/Foundation.h>
  • import "ECDeviceHeaders.h" 
  • @interface DeviceDelegateHelper : NSObject
  • /**
  •  *@brief 获取DeviceDelegateHelper单例句柄
  •  */
  • +(DeviceDelegateHelper*)sharedInstance; 
  •                
                

2.2 DeviceDelegateHelper.m文件                      

2.2.1 创建单例方法

  • //代理类.m文件中需要实现ECDeviceDelegate的回调函数,代码示例如下:
  • import "DeviceDelegateHelper.h"
  • //如需使用IM功能,需实现ECChatDelegate类的回调函数。
  • //如需使用实时音视频功能,需实现ECVoIPCallDelegate类的回调函数。
  • //如需使用音视频会议功能,需实现ECMeetingDelegate类的回调函数。 
  • +(DeviceDelegateHelper*)sharedInstance
  • {
  •     static DeviceDelegateHelper *devicedelegatehelper;

  •     static dispatch_once_t devicedelegatehelperonce;

  •     dispatch_once(&devicedelegatehelperonce, ^{

  •         devicedelegatehelper = [[DeviceDelegateHelper alloc] init];  
  •     });
  •     return devicedelegatehelper;

2.2.2 连接云通讯的服务平台,实现ECDelegateBase代理的方法

  • /**
  •  @brief 连接状态接口
  •  @discussion 监听与服务器的连接状态 V5.0版本接口
  •  @param state 连接的状态
  •  @param error 错误原因值
  •  */
  • -(void)onConnectState:(ECConnectState)state failed:(ECError*)error {

  •     switch (state) {

  •         case State_ConnectSuccess://连接成功

  •             [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_onConnected object:
  •             [ECError errorWithCode:ECErrorType_NoError]];

  •             break;

  •         case State_Connecting://正在连接

  •             [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_onConnected object:
  •             [ECError errorWithCode:ECErrorType_Connecting]];

  •             break;

  •         case State_ConnectFailed://失败

  •             [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_onConnected object:error];

  •             break;
  •         default:
  •             break;
  •     }
  • }

2.2.3 各功能回调函数实现

1. im相关功能回调
  • /**
  •  @brief 客户端录音振幅代理函数
  •  @param amplitude 录音振幅
  •  */
  • -(void)onRecordingAmplitude:(double) amplitude; 

  • /**
  •  @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 接收群组相关消息
  •  @discussion 参数要根据消息的类型,转成相关的消息类(解散群组、收到邀请、申请加入、退出群组、有人加入、移除成员等消息)  
  •  @param groupMsg 群组消息
  •  */
  • -(void)onReceiveGroupNoticeMessage:(ECGroupNoticeMessage *)groupMsg;

2.点对点音视频相关功能回调
  • /**
  •  @brief 有呼叫进入
  •  @param callid      会话id
  •  @param caller      呼叫人
  •  @param callerphone 被叫人手机号
  •  @param callername  被叫人姓名
  •  @param calltype    呼叫类型
  •  */
  • - (NSString*)onIncomingCallReceived:(NSString*)callid withCallerAccount:(NSString *)caller withCallerPhone:(NSString *)callerphone withCallerName:(NSString *)callername withCallType:(CallType)calltype;
  •  
  • /**
  •  @brief 呼叫事件
  •  @param voipCall 电话实体类的对象
  •  */
  • - (void)onCallEvents:(VoIPCall*)voipCall;
  •  
  • /**
  •  @brief 收到dtmf
  •  @param callid 会话id
  •  @param dtmf   键值
  •  */
  • - (void)onReceiveFrom:(NSString*)callid DTMF:(NSString*)dtmf;

  • /**
  •  @brief 视频分辨率发生改变
  •  @param callid       会话id
  •  @param voip         通讯号
  •  @param isConference NO 不是, YES 是
  •  @param width        宽度
  •  @param height       高度
  •  */
  • - (void)onCallVideoRatioChanged:(NSString *)callid andVoIP:(NSString *)voip andIsConfrence:(BOOL)isConference andWidth:(NSInteger)width andHeight:(NSInteger)height;

  • /**
  •  @brief 收到对方切换音视频的请求
  •  @param callid  会话id
  •  @param requestType 请求音视频类型 视频:需要响应 音频:请求删除视频(不需要响应,双方自动去除视频)
  •  */
  • - (void)onSwitchCallMediaTypeRequest:(NSString *)callid withMediaType:(CallType)requestType;

  • /**
  •  @brief 收到对方应答切换音视频请求
  •  @param callid   会话id
  •  @param responseType 回复音视频类型
  •  */
  • - (void)onSwitchCallMediaTypeResponse:(NSString *)callid withMediaType:(CallType)responseType;

3.音视频会议相关功能回调
  • /**
  •  @brief 实时对讲通知消息
  •  @param msg 实时对讲消息
  •  */
  • -(void)onReceiveInterphoneMeetingMsg:(ECInterphoneMeetingMsg *)msg;
  •  
  • /**
  •  @brief 语音群聊通知消息
  •  @param msg 语音群聊消息
  •  */
  • -(void)onReceiveMultiVoiceMeetingMsg:(ECMultiVoiceMeetingMsg *)msg;

  • //多路视频通知消息
  • -(void)onReceiveMultiVideoMeetingMsg:(ECMultiVideoMeetingMsg *)msg;

  • //有会议呼叫邀请。邀请用户时,呼叫回调接口,与点对点外呼接口不一致,只有会议邀请才会回调这个接口,其他情况均使用点对点呼叫回调接口
  • -(NSString*)onMeetingCallReceived:(NSString*)callid withCallType:(CallType)calltype withMeetingData:(NSDictionary*)meetingData;                  
                
                

3、登录

   
初始化成功之后我们就可以进行登录。登录有三种模式,我们选择其中一种登录方式即可:
(1)任意帐号体系(默认模式) 
(2)固定帐号体系(密码模式)
(3)任意账号体系(鉴权模式)
注意:推荐使用(1)自定义登录方式。因为此方式下云通讯平台不验证用户的登录密码,只进行应用级的验证。提高客户帐号创建的自由度和使用的便捷性。
示例代码如下:

3.1 任意帐号体系(默认模式)                                          

  • //初始化登录信息类ECLoginInfo实例(ECLoginInfo已经包含在SDK包里,不要用户创建)

  • //默认模式:对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{
  •        //登录失败
  •     }
  • }];

3.2 固定帐号体系(密码模式)


  • //密码模式:对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{
  •        //登录失败
  •     }
  • }];

3.3 任意帐号体系(鉴权模式)

  • // 鉴权模式:MD5 Token认证方式,服务器认证appKey、username、timestamp、MD5Token字段
  • //* (该鉴权方式是最安全的方式,用户在自己的服务器根据规则生成MD5,在不暴露apptoken的情况下进行鉴权,且生成的MD5 Token在平台上有失效时间)
  • ECLoginInfo * loginInfo = [[ECLoginInfo alloc] init];
  • loginInfo.username = @"自定义账号";
  • loginInfo.appKey = @"appid";
  • loginInfo.timestamp = "时间戳";//yyyyMMddHHmmss 
  • loginInfo.MD5Token = "MD5串";//MD5(appid+username+timestamp+apptoken)
  • loginInfo.authType = LoginAuthType_MD5TokenAuth;// 鉴权方式登录
  • loginInfo.mode = LoginMode_InputPassword;
  • [[ECDevice sharedInstance] login:loginInfo completion:^(ECError *error){
  •     if (error.errorCode == ECErrorType_NoError) {
  •        //登录成功
  •     }else{
  •        //登录失败
  •     }
  • }];
             
登录环境:
登录环境分为:生产(正式)环境和沙盒环境。主要是推送证书不一样。如果你是在开发环境注册的推送,相对应就需要选择在沙盒环境上登录;如果你是生产环境注册的推送,相对应就需要选择在生产(正式)环境上登录。
注:提供此方法仅用于客户测试IOS离线推送功能,在测试完成后请及时去掉,防止环境不同导致的通信障碍。         
  • 功能:切换服务器环境
  • 调用登录接口前,调用该接口切换服务器环境;不调用该函数,默认使用的是生产环境;
  • 参数:isSandBox 是否沙盒环境
  •      是否成功 0:成功 非0失败
  • -(NSInteger)SwitchServerEvn:(BOOL)isSandBox;