iOS 即时通讯容联云SDK的集成

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS 即时通讯容联云SDK的集成相关的知识,希望对你有一定的参考价值。

 

1、初始化及登陆


在程序入口初始化SDK并设置代理 - 代理类设置最好放在didFinishLaunchingWithOptions中,代码示例如下:


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


   #warning 设置代理


   [ECDevice sharedInstance].delegate = [DeviceDelegateHelper sharedInstance];


   return YES;


}


 


代理类示例:


DeviceDelegateHelper.h文件   Class->Model


// 代理类.h文件如下


#import <Foundation/Foundation.h>


#import "ECDeviceHeaders.h"


#import "AppDelegate.h"


@interface DeviceDelegateHelper : NSObject<ECDeviceDelegate>


/**


 *@brief 获取DeviceDelegateHelper单例句柄


 */


 


+ (DeviceDelegateHelper*)sharedInstance;


 


DeviceDelegateHelper.m文件


/**


 * 第一步:创建单例方法


 *


 */


+ (DeviceDelegateHelper*)sharedInstance{


    static DeviceDelegateHelper *devicedelegatehelper;


    static dispatch_once_t devicedelegatehelperonce;


    dispatch_once(&devicedelegatehelperonce, ^{


        devicedelegatehelper = [[DeviceDelegateHelper alloc] init];


    });


    return devicedelegatehelper;


}


 


/**


 * 第二步:连接云通讯的服务平台,实现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;


    }


}


 


第三步:各功能回调函数实现


/**


 @brief 客户端录音振幅代理函数


 @param amplitude 录音振幅


 */


- (void)onRecordingAmplitude:(double) amplitude{


    


    [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_onRecordingAmplitude object:@(amplitude)];


}


 


/**


 @brief 接收即时消息代理函数


 @param message 接收的消息


 */


- (void)onReceiveMessage:(ECMessage*)message{


    


    if (message.from.length==0 || message.messageBody.messageBodyType==MessageBodyType_Call) {


        return;


    }


    


    if (message.messageBody.messageBodyType == MessageBodyType_Text) {


        ECTextMessageBody * textmsg = (ECTextMessageBody *)message.messageBody;


        textmsg.text = [[EmojiConvertor sharedInstance] convertEmojiSoftbankToUnicode:textmsg.text];


    }


    


#warning 时间全部转换成本地时间


    if (message.timestamp) {


        NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];


        NSTimeInterval tmp =[date timeIntervalSince1970]*1000;


        message.timestamp = [NSString stringWithFormat:@"%lld", (long long)tmp];


    }


    


    [[DeviceDBHelper sharedInstance] addNewMessage:message andSessionId:self.sessionId];


    


    //同步过来的发送消息不播放提示音


    if (message.messageState == ECMessageState_Receive) {


        [self playRecMsgSound:message.sessionId];


    }


    


    [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_onMesssageChanged object:message];


    


    MessageBodyType bodyType = message.messageBody.messageBodyType;


    if( bodyType == MessageBodyType_Voice || bodyType == MessageBodyType_Video || bodyType == MessageBodyType_File || bodyType == MessageBodyType_Image){


        ECFileMessageBody *body = (ECFileMessageBody*)message.messageBody;


        body.displayName = body.remotePath.lastPathComponent;


        


        if (message.messageBody.messageBodyType == MessageBodyType_Video) {


            


            ECVideoMessageBody *videoBody = (ECVideoMessageBody *)message.messageBody;


            


            if (videoBody.thumbnailRemotePath == nil) {


                videoBody.displayName = videoBody.remotePath.lastPathComponent;


                [[DeviceChatHelper sharedInstance] downloadMediaMessage:message andCompletion:nil];


            }


            


        } else {


            [[DeviceChatHelper sharedInstance] downloadMediaMessage:message andCompletion:nil];


        }


    }


}


 


 


/**


 @brief 离线消息数


 @param count 消息数


 */


- (void)onOfflineMessageCount:(NSUInteger)count{


    NSLog(@"onOfflineMessageCount=%lu",(unsigned long)count);


    self.offlineCount = count;


}


 


/**


 @brief 需要获取的消息数


 @return 消息数 -1:全部获取 0:不获取


 */


- (NSInteger)onGetOfflineMessage{


    NSInteger retCount = -1;


    if (self.offlineCount!=0) {


        /*


        if (self.offlineCount>100) {


            retCount = 100;


        }


        */


        dispatch_async(dispatch_get_main_queue(), ^{


            [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_haveHistoryMessage object:nil];


        });


    }


    return retCount;


}


 


 


/**


 @brief 接收离线消息代理函数


 @param message 接收的消息


 */


- (void)onReceiveOfflineMessage:(ECMessage*)message{


    if (message.from.length==0 || message.messageBody.messageBodyType==MessageBodyType_Call) {


        if (message.messageBody.messageBodyType==MessageBodyType_Call) {


            [[DeviceDBHelper sharedInstance] addNewMessage:message andSessionId:self.sessionId];


            [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_onMesssageChanged object:message];


        }


        return;


    }


    


    if (message.messageBody.messageBodyType == MessageBodyType_Text) {


        ECTextMessageBody * textmsg = (ECTextMessageBody *)message.messageBody;


        textmsg.text = [[EmojiConvertor sharedInstance] convertEmojiSoftbankToUnicode:textmsg.text];


    }


    


#warning 时间全部转换成本地时间


    if (!message.timestamp) {


        NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];


        NSTimeInterval tmp =[date timeIntervalSince1970]*1000;


        message.timestamp = [NSString stringWithFormat:@"%lld", (long long)tmp];


    }


    


    [[DeviceDBHelper sharedInstance] addNewMessage:message andSessionId:self.sessionId];


    


    [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_onMesssageChanged object:message];


    


    MessageBodyType bodyType = message.messageBody.messageBodyType;


    if( bodyType == MessageBodyType_Voice || bodyType == MessageBodyType_Video || bodyType == MessageBodyType_File || bodyType == MessageBodyType_Image){


        ECFileMessageBody *body = (ECFileMessageBody*)message.messageBody;


        body.displayName = body.remotePath.lastPathComponent;


        


        if (message.messageBody.messageBodyType == MessageBodyType_Video) {


            ECVideoMessageBody *videoBody = (ECVideoMessageBody *)message.messageBody;


            if (videoBody.thumbnailRemotePath == nil) {


                videoBody.displayName = videoBody.remotePath.lastPathComponent;


                [[DeviceChatHelper sharedInstance] downloadMediaMessage:message andCompletion:nil];


            }


        } else {


            [[DeviceChatHelper sharedInstance] downloadMediaMessage:message andCompletion:nil];


        }


    }


}


 


 


/**


 @brief 离线消息接收是否完成


 @param isCompletion YES:拉取完成 NO:拉取未完成(拉取消息失败)


 */


- (void)onReceiveOfflineCompletion:(BOOL)isCompletion {


    dispatch_async(dispatch_get_main_queue(), ^{


        [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_HistoryMessageCompletion object:nil];


    });


    [self playRecMsgSound:nil];


}


 


/**


 @brief 接收群组相关消息


 @discussion 参数要根据消息的类型,转成相关的消息类;


 解散群组、收到邀请、申请加入、退出群组、有人加入、移除成员等消息


 @param groupMsg 群组消息


 */


- (void)onReceiveGroupNoticeMessage:(ECGroupNoticeMessage *)groupMsg{


    


#warning 时间全部转换成本地时间


    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];


    NSTimeInterval tmp =[date timeIntervalSince1970]*1000;


    groupMsg.dateCreated = [NSString stringWithFormat:@"%lld", (long long)tmp];


    


    [[DeviceDBHelper sharedInstance] addNewGroupMessage:groupMsg];


    if (groupMsg.messageType ==ECGroupMessageType_Dissmiss) {


        


        [[DeviceDBHelper sharedInstance] deleteAllMessageOfSession:groupMsg.groupId];


        


    } else if (groupMsg.messageType == ECGroupMessageType_RemoveMember) {


        


        ECRemoveMemberMsg *message = (ECRemoveMemberMsg *)groupMsg;


        if ([message.member isEqualToString:[DemoGlobalClass sharedInstance].userName]) {


            [[DeviceDBHelper sharedInstance] deleteAllMessageOfSession:groupMsg.groupId];


        }


    }


    [self playRecMsgSound:nil];


    


    [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_onReceivedGroupNotice object:groupMsg];


}


 

 

以上是关于iOS 即时通讯容联云SDK的集成的主要内容,如果未能解决你的问题,请参考以下文章

IOS集成融云SDK即时通讯

有人用过腾讯通讯云IM吗

融云即时通讯SDK集成 -- FCM推送集成指南(Android平台)

融云即时通讯SDK集成 -- 国内厂商推送集成踩坑篇(Android平台)

融联云通讯的底层实现协议简介

集成融云即时通讯碰到的一些问题