1. 程式人生 > >iOS整合容聯雲通訊 IM

iOS整合容聯雲通訊 IM

iOS整合雲通訊 IM

  • 在雲通訊平臺註冊帳號及建立應用,在‘管理控制檯’中的‘控制檯首頁’可以看到‘開發者資訊’和‘開發者主帳號’。在應用列表中可以看到‘APP ID’和‘APP TOKEN’,這兩個一會兒會用到。

  • 建立工程,將 SDK 新增到專案中,並新增依賴庫檔案,參考這個文件。然後執行工程,看是否將依賴庫新增到工程中。

  • 接下來建立一個類:DeviceDelegateHelper

DeviceDelegateHelper.h

#import <Foundation/Foundation.h>
#import <Foundation/Foundation.h>

#import
"IMSDK/ECDevice.h" @interface DeviceDelegateHelper : NSObject<ECDeviceDelegate> /** * 獲取DeviceDelegateHelper 單例控制代碼 * * @return 單例 */ + (DeviceDelegateHelper *)sharedInstance; @end

DeviceDelegateHelper.m

#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 類中新增登入方法,寫一個按鈕觸發這個方法
- (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 類中後,用兩個手機執行程式,記住登入帳號和傳送帳號要修改,切記傳送內容不能為空,實現程式碼如下
- (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(@"傳送失敗");
        }
    }];

}

至此,即可用兩個手機做測試,一個手機加斷點,傳送資訊時看控制檯的輸出。