本文介绍iOS互动SDK的支持系统、集成方式以及部分功能的使用。

支持系统

iOS支持iOS 8.0及以上,Xcode 11.2.1。

macOS支持OS X 10.12及以上。

配置环境

  1. 复制文件夹中libs里的framework依赖,粘贴到项目中的libs目录里。
    libinteraction.framework
    BabaSSL
    GaeaNetwork.framework
    libaim.framework
    libdmojo_support.framework
    libdps.framework
    xlite.framework
  2. 添加系统库。
    libz.tdb
    libresolv.tbd
    libc++.tbd
    CoreTelephony.framework
    SystemConfigation.framework

公共部分工程接入

  1. 初始化引擎。
     DPSPubEngine  * _engine = [DPSPubEngine CreateDPSEngine];
        // 初始化模块
      DPSModuleInfo *moduleInfo = InteractionModule.getModuleInfo;// = InteractionModule.getModuleInfo();
      [_engine registerModule:moduleInfo];
    
      [_engine startWithBlock:^(void){
          NSLog(@"engine init success");
          [self addLog:@"初始化成功!"];
    
      } onFailure:^(DPSError * _Nonnull error){
          NSLog(@"error is %@",error);
          [self addLog:[NSString stringWithFormat:@"错误:%@",error.reason]];
      }];

    其中initService做各种信息配置。

      // 获取配置服务
      DPSPubSettingService *setting = _engine.getSettingService;
      [setting setDataPath:[Environments shareInstance].dataPath];
      [setting setAppKey:APP_KEY];
      DPSMediaHost *host = [DPSMediaHost DPSMediaHostWithType:DPSMediaHostTypeMediaHostTypeAuth host:MEDIA_HOST];
      [setting setMediaHost:@[host]];
      [setting setEnvType:DPSEnvTypeEnvTypePreRelease];
      [setting setLonglinkServerAddress:LONG_LINK_ADDR];
      [setting setOSName:[Environments shareInstance].osName];
      [setting setAppName:[Environments shareInstance].appName];
      [setting setAppVersion:[Environments shareInstance].appVersion];
      [setting setOSVersion:[Environments shareInstance].osVersion];
      [setting setDisableSslVerify:YES];
      [setting setDeviceId:[Environments shareInstance].deviceId];
      [setting setDeviceName:[Environments shareInstance].deviceName];
      [setting setDeviceType:[Environments shareInstance].deviceType];
      [setting setAppID:@"apitest"];
    
      [setting setAuthTokenCallback:self];
                        
  2. 创建Manager。
      DPSUserId *dpsUserId = [[DPSUserId alloc] init];
      dpsUserId.uid = _useridInput.text;
      dpsUserId.domain = [Environments shareInstance].domainName;
      
      [_engine createDPSManagerWithBlock:dpsUserId.uid onSuccess:^(DPSPubManager * _Nullable manager) {
          NSLog(@"manager is %@",manager);
          if (manager) {
              self.DPSPubManager = manager;
              [self addLog:@"创建manager成功!"];
          }else{
              [self addLog:[NSString stringWithFormat:@"错误:%@",@"创建manager失败!"]];
          }
    
          InteractionModule  *moduleManager = [InteractionModule getInteractionManager:dpsUserId.uid];
          self.moduleManager = moduleManager;
          
          InteractionMessageServiceInterface *messageService = [self.moduleManager getMessageService];
          [messageService setListener:self];
    
          NSLog(@"InteractionModule MANAGER 1 IS %@",moduleManager);
          NSLog(@"manager1.getRoomService is %@",moduleManager.getRoomService);
          NSLog(@"manager1.getMessageService is %@",moduleManager.getMessageService);
    
      } onFailure:^(DPSError * _Nonnull error) {
          NSLog(@"error is %@",error);
          [self addLog:[NSString stringWithFormat:@"错误:%@",error.reason]];
    
      }];
  3. 登录认证。
      DPSUserId *dpsUserId = [[DPSUserId alloc] init];
      dpsUserId.uid = _useridInput.text;
      dpsUserId.domain = [Environments shareInstance].domainName;
    
      InteractionModule  *manager = [InteractionModule getInteractionManager:dpsUserId.uid];
      
      NSString *uid = manager.getUid;
    
      if (!uid) {
          NSLog(@"获取userId失败, 请先创建Manager");
          [self.view makeToast:@"获取userId失败, 请先创建Manager"];
          return;
      }else{
          if (![uid isEqualToString:_useridInput.text]) {
              NSLog(@"uid不一致,引擎实例不一致");
              [self.view makeToast:@"uid不一致,引擎实例不一致"];
              return;
          }else{
              // 获取引擎
              DPSPubEngine *engine = [DPSPubEngine GetDPSEngine];
              if (!engine) {
                  NSLog(@"engine is null");
                  [self.view makeToast:@"engine is null"];
                  return;
              }
              // 获取互动业务Manager
              DPSPubManager *DPSPubManager = [engine getDPSManager:dpsUserId.uid];
              if (!DPSPubManager) {
                  [self.view makeToast:@"DPSPubManager is null"];
                  NSLog(@"DPSPubManager is null");
                  return;
              }
              // 获取授权登录Service
              DPSAuthService *authService = [DPSPubManager getAuthService];
              if (!authService) {
                  [self.view makeToast:@"authService is null"];
                  NSLog(@"authService is null");
                  return;
              }
              DPSConnectionStatus authStatus = [authService getConnectionStatus];
              // 获取连接状态
              if (authStatus == DPSConnectionStatusCsAuthed) {
                  NSLog(@"已经登录!");
                  [self addLog:@"已经登录"];
                  return;
              }
              // 注册监听
              [authService addListener:self];
              // 发起登录
              [authService login];
          }
      }
    ```
                        

服务部分工程接入

服务部分包括房间服务和消息服务。

房间部分
  1. 获取房间服务。
      InteractionRoomServiceInterface *roomService = [self.moduleManager getRoomService];
      InteractionGetRoomDetailReq *request = [[InteractionGetRoomDetailReq alloc] initWithRoomId:_queryRoomIDInput.text?_queryRoomIDInput.text: TEST_ROOM_ID];
    
      [roomService getRoomDetailWithBlock:request onSuccess:^(InteractionGetRoomDetailRsp * _Nonnull rsp) {
          NSLog(@"查询房间成功");
          NSString *title = rsp.title;
          NSString *ownerNick = rsp.ownerNick;
          NSString *notice = rsp.notice;
          NSString *uv = [NSString stringWithFormat:@"%d",rsp.uv];
          NSString *onlineCount =  [NSString stringWithFormat:@"%d",rsp.onlineCount];
          NSString *likesCount =  [NSString stringWithFormat:@"%lld",rsp.likesCount];
          [self addLog:@"查询房间成功!"];
          [self addLog:[NSString stringWithFormat:@"房间名:%@   房主名:%@   公告:%@   UV :%@  在线数:%@  点赞数:%@",title,ownerNick,notice,uv,onlineCount,likesCount]];
    
      } onFailure:^(DPSError * _Nonnull error) {
          NSLog(@"查询房间失败!");
          [self addLog:[NSString stringWithFormat:@"错误:查询房间失败 错误详情:%@",error.reason]];
      }];
  2. 进入房间。
      InteractionRoomServiceInterface *roomService = [self.moduleManager getRoomService];
      InteractionEnterRoomReq *request = [[InteractionEnterRoomReq alloc] initWithRoomId:_accessRoomIDInput.text? _accessRoomIDInput.text: TEST_ROOM_ID nick:@"PKTest"];
      if (!request.roomId) {
          [self.view makeToast:@"请输入RoomId"];
          return;
      }
      [roomService enterRoomWithBlock:request onSuccess:^{
          NSLog(@"进入房间成功");
          [self addLog:@"进入房间成功!"];
      } onFailure:^(DPSError * _Nonnull error) {
          NSLog(@"进入房间失败!");
      
          [self addLog:[NSString stringWithFormat:@"错误:进入房间失败! 错误详情:%@",error.reason]];
    
      }];
  3. 发布房间公告。
      InteractionRoomServiceInterface *roomService = [self.moduleManager getRoomService];
      
      InteractionPublishNoticeReq *request = [[InteractionPublishNoticeReq alloc] initWithRoomId:_sendMSGRoomIDInput.text?_accessRoomIDInput.text :TEST_ROOM_ID notice:@"notice"];
      if (!request.roomId) {
          [self.view makeToast:@"请输入RoomId"];
          return;
      }
      if (!request.notice) {
          [self.view makeToast:@"请输入公告内容"];
          return;
      }
      [roomService publishRoomNoticeWithBlock:request onSuccess:^{
          [self addLog:@"发布房间公告成功!"];
    
      } onFailure:^(DPSError * _Nonnull error) {
      
          [self addLog:[NSString stringWithFormat:@"错误:发布房间公告失败 错误详情:%@",error.reason]];
    
      }];
  4. 离开房间。
      InteractionRoomServiceInterface *roomService = [self.moduleManager getRoomService];
      
      InteractionLeaveRoomReq *request = [[InteractionLeaveRoomReq alloc] initWithRoomId:TEST_ROOM_ID];
      if (!request.roomId) {
          [self.view makeToast:@"请输入RoomId"];
          return;
      }
      [roomService leaveRoomWithBlock:request onSuccess:^{
          [self addLog:@"离开房间成功!"];
    
      } onFailure:^(DPSError * _Nonnull error) {
    
          [self addLog:[NSString stringWithFormat:@"错误:离开房间失败! 错误详情:%@",error.reason]];
    
      }];
消息部分
  1. 获取消息服务。
    InteractionMessageServiceInterface *messageService = [self.moduleManager getMessageService];
  2. 点赞。
      InteractionMessageServiceInterface *messageService = [self.moduleManager getMessageService];
      
      InteractionSendLikesReq *request = [[InteractionSendLikesReq alloc] initWithRoomId:_queryRoomIDInput.text?_queryRoomIDInput.text: TEST_ROOM_ID count:1];
      if (!request.roomId) {
          [self.view makeToast:@"请输入RoomId"];
          return;
      }
      [messageService sendLikesWithBlock:request onSuccess:^(InteractionSendLikesRsp * _Nonnull rsp) {
          [self addLog:[NSString stringWithFormat:@"点赞消息成功! 间隔:%d",rsp.interval]];
    
      } onFailure:^(DPSError * _Nonnull error) {
          [self addLog:[NSString stringWithFormat:@"错误:点赞消息失败! 错误详情:%@",error.reason]];
      }];
  3. 发文本消息。
      InteractionMessageServiceInterface *messageService = [self.moduleManager getMessageService];
      InteractionSendCommentReq *request = [[InteractionSendCommentReq alloc] initWithRoomId: TEST_ROOM_ID type:0 content:_sendMSGRoomIDInput.text?_sendMSGRoomIDInput.text:@"Just a test." extension:@{@"":@""}];
      if (!request.roomId) {
          [self.view makeToast:@"请输入RoomId"];
          return;
      }
      if (!request.content) {
          [self.view makeToast:@"请输入消息内容"];
          return;
      }
      [messageService sendCommentWithBlock:request onSuccess:^(InteractionSendCommentRsp * _Nonnull rsp) {
          [self addLog:[NSString stringWithFormat:@"发送消息成功! 评论id:%@ 间隔:%d",rsp.commentId,rsp.interval]];
    
      } onFailure:^(DPSError * _Nonnull error) {
          [self addLog:[NSString stringWithFormat:@"错误:发送消息失败! 错误详情:%@",error.reason]];
      }];
  4. 发自定义消息。
      InteractionMessageServiceInterface *messageService = [self.moduleManager getMessageService];
      
      NSString *subtype = _sendCustomMSGSubTypeInput.text? _sendCustomMSGSubTypeInput.text:@"";
      int subtypeInt = 0;
      if (![subtype isEqualToString:@""] &&[subtype intValue] > 0) {
          subtypeInt = [subtype  intValue];
      }else if ([subtype isEqualToString:@""]){
          subtypeInt = 100001;
      }
      InteractionSendCustomMessageReq *request = // [[InteractionSendCustomMessageReq alloc] initWithRoomId:_sendCustomMSGBodyInput.text?_sendCustomMSGBodyInput.text: TEST_ROOM_ID subType: subtypeInt body:@"customMessageBody" receivers:@[]];
      
      [[InteractionSendCustomMessageReq alloc] initWithRoomId:TEST_ROOM_ID subType:subtypeInt body:_sendCustomMSGBodyInput.text priority:0 receivers:@[]];
      
      if (!request.roomId) {
          [self.view makeToast:@"请输入RoomId"];
          return;
      }
      if (!request.body) {
          [self.view makeToast:@"请输入自定义消息内容"];
          return;
      }
      [messageService sendCustomMessageWithBlock:request onSuccess:^(InteractionSendCustomMessageRsp * _Nonnull rsp) {
          [self addLog: [NSString stringWithFormat:@"自定义消息成功! 消息id: %@",rsp.msgId]];
      } onFailure:^(DPSError * _Nonnull error) {
          [self addLog:[NSString stringWithFormat:@"错误:自定义消息失败! %@",error.reason]];
      }];
                        
  5. 查询消息。
      InteractionMessageServiceInterface *messageService = [self.moduleManager getMessageService];
      
      InteractionQueryCommentReq *request = [[InteractionQueryCommentReq alloc] initWithRoomId:_queryMsgIDInput.text?_queryMsgIDInput.text: TEST_ROOM_ID];
      if (!request.roomId) {
          [self.view makeToast:@"请输入RoomId"];
          return;
      }
    
      [messageService queryHistoryMessageWithBlock:request onSuccess:^(InteractionQueryCommentRsp * _Nonnull rsp) {
          for (int i = 0; i < rsp.msgList.count; i ++) {
              InteractionCommentModel *model = rsp.msgList[i];
              [self addLog:[NSString stringWithFormat:@"查询消息:%@",model.content]];
              NSLog(@"查询消息 %@",model.content);
          }
      } onFailure:^(DPSError * _Nonnull error) {
          [self addLog:[NSString stringWithFormat:@"错误:查询消息失败! %@",error.reason]];
      }];

日志与回调

  • 日志输出。
    - (void)onLog:(DPSLogLevel)logLevel logContent:(nonnull NSString *)logContent{
        NSString *content = [NSString stringWithFormat:@"%@",logContent];
        NSLog(@"content is %@",content);
    }
  • 登录状态回调监听。
    - (void)onConnectionStatusChanged:(DPSConnectionStatus)status{
        NSLog(@"onConnectionStatusChanged status is %ld",status);
        if (status == DPSConnectionStatusCsAuthed) {
            [self addLog:@"已经登录"];
        }
    }
    - (void)onGetAuthCodeFailed:(int32_t)errorCode errorMsg:(nonnull NSString *)errorMsg{
        [self addLog:[NSString stringWithFormat:@"错误:%@",errorMsg]];
    }
    - (void)onLocalLogin{
    }
    - (void)onKickout:(nonnull NSString *)message{
    }
    - (void)onDeviceStatus:(int32_t)type deviceType:(int32_t)deviceType status:(int32_t)status time:(int64_t)time{
    }
    - (void)onMainServerCookieRefresh:(nonnull NSString *)cookie{
    }
  • 登录回调。
    - (void)onCallback:(nonnull NSString *)userId
     onGot:(nullable DPSAuthTokenGotCallback *)onGot
                reason:(DPSAuthTokenExpiredReason)reason{
        NSLog(@"reason IS %d",reason);
        __weak ViewController *weakSelf = self;
        [[AIInteractionManager shareInstance] requestTokenWithCompletion:userId completion:^(DPSAuthToken * _Nullable authToken, NSError * _Nullable error) {
            weakSelf.authToken = authToken;
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"AuthToken accessToken is %@",authToken.accessToken);
                NSLog(@"AuthToken refreshToken is %@",authToken.refreshToken);
                if (authToken.accessToken.length > 0 || authToken.refreshToken.length > 0) {
                    [onGot onSuccess:authToken];
                    [self addLog:@"登录成功!"];
                }else{
                    [onGot onFailure:(int)error.code errorMsg:error.userInfo.description];
                    NSLog(@"登录失败!");
                    [self addLog:[NSString stringWithFormat:@"错误:%@",@"登录失败"]];
                }
            });
        }];
    }
  • 消息回调。
    - (void)onCommentNtf:(nonnull DPSUserId *)uid
                  roomId:(nonnull NSString *)roomId
                     ntf:(nonnull InteractionCommentNtf *)ntf{
        [self addLog:[NSString stringWithFormat:@"接收评论: %@ %@",ntf.nick,ntf.content]];
    }
    - (void)onLikesCountNtf:(nonnull DPSUserId *)uid
                     roomId:(nonnull NSString *)roomId
                        ntf:(nonnull InteractionLikesNtf *)ntf{
        [self addLog:[NSString stringWithFormat:@"接收点赞: %lld",ntf.likeCount]];
    }
    - (void)onCustomMessageNtf:(nonnull DPSUserId *)uid
                        roomId:(nonnull NSString *)roomId
                           ntf:(nonnull InteractionCustomMsgNtf *)ntf{
        [self addLog:[NSString stringWithFormat:@"接收到自定义消息: %@",ntf.body]];
    }