您的位置:首页 > 移动开发 > IOS开发

iOS集成容联云通信 IM

2015-08-07 17:54 471 查看

iOS集成云通信 IM

在云通信平台注册帐号及创建应用,在‘管理控制台’中的‘控制台首页’可以看到‘开发者信息’和‘开发者主帐号’。在应用列表中可以看到‘APP ID’和‘APP TOKEN’,这两个一会儿会用到。

下载 SDK 和 Demo

创建工程,将 SDK 添加到项目中,并添加依赖库文件,参考这个文档。然后运行工程,看是否将依赖库添加到工程中。

接下来创建一个类:
DeviceDelegateHelper


DeviceDelegateHelper.h

[code]#import <Foundation/Foundation.h>
#import <Foundation/Foundation.h>

#import "IMSDK/ECDevice.h"

@interface DeviceDelegateHelper : NSObject<ECDeviceDelegate>

/**
 *  获取DeviceDelegateHelper 单例句柄
 *
 *  @return 单例
 */
+ (DeviceDelegateHelper *)sharedInstance;

@end


DeviceDelegateHelper.m

[code]#import "DeviceDelegateHelper.h"

@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
{
    NSLog(@"收到消息啦");
}

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

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

/**
 @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


在 ViewController 类中添加登录方法,写一个按钮触发这个方法

[code]- (IBAction)nextBtnClicked:(id)sender {
    ECLoginInfo * loginInfo = [[ECLoginInfo alloc] init];
    loginInfo.username = @"18612345678";
    loginInfo.userPassword = nil;
    loginInfo.appKey = @"你应用的 appkey";
    loginInfo.appToken = @"你应用的 appToken";
    loginInfo.authType = LoginAuthType_NormalAuth;
    loginInfo.mode = LoginMode_InputPassword;

    [[ECDevice sharedInstance] login:loginInfo completion:^(ECError *error){

        if (error.errorCode == ECErrorType_NoError) {
            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
            OneToOneChatVC *oneToOneVC = [storyboard instantiateViewControllerWithIdentifier:@"OneToOneChatVC"];
            [self.navigationController pushViewController:oneToOneVC animated:YES];
        }
    }];
}


跳转到 OneToOneChatVC 类中后,用两个手机运行程序,记住登录帐号和发送帐号要修改,切记发送内容不能为空,实现代码如下

[code]- (IBAction)sendBtn:(id)sender {
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval tmp =[date timeIntervalSince1970]*1000;

    ECTextMessageBody *messageBody = [[ECTextMessageBody alloc] initWithText:_sendTextField.text];
    ECMessage *message = [[ECMessage alloc] initWithReceiver:@"18612345678" body:messageBody];
//    ECMessage *message = [[ECMessage alloc] initWithReceiver:@"18687654321" body:messageBody];

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

    [[ECDevice sharedInstance].messageManager sendMessage:message progress:self completion:^(ECError *error, ECMessage *amessage) {

        if (error.errorCode == ECErrorType_NoError) {
            NSLog(@"发送成功");
        }else if(error.errorCode==ECErrorType_Have_Forbid||error.errorCode==ECErrorType_File_Have_Forbid) {
            NSLog(@"您已被群组禁言");
        }else{
            NSLog(@"发送失败");
        }
    }];

}


至此,即可用两个手机做测试,一个手机加断点,发送信息时看控制台的输出。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: