iOS

初始化与登录

更新时间: 2024/03/15 18:26:23

在使用圈组 UIKit 功能前,必须先对其进行初始化并完成登录,但无需要对 NIM SDK 单独初始化。

圈组 UIKit 的核心类 QChatKitClient,提供初始化、登录、获取当前账号信息等能力。

前提条件

初始化

  1. 在项目中引入需要的组件。

    示例代码:

    Swift
    import NECoreKit
    import NECoreIMKit
    import NECoreQChatKit
    import NEQChatUIKit
    import NERtcCallUIKit
    ...
    
    Objective-C
    #import <NECoreKit/NECoreKit-Swift.h>
    #import <NECoreIMKit/NECoreIMKit-Swift.h>
    #import <NECoreQChatKit/NECoreQChatKit-Swift.h>
    #import <NEQChatKit/NEQChatKit-Swift.h>
    #import <NEQChatUIKit/NEQChatUIKit-Swift.h>
    #import <NERtcCallUIKit/NERtcCallUIKit.h>
    ...
    
  2. 在应用启动时,通过调用setupCoreKitQChat方法实现初始化。

初始化必须在应用的生命周期内进行,且只可进行一次。

  • 方法原型

    Swift
    // QChat 初始化
    public func setupCoreKitQChat(_ option: NIMSDKOption)
    
    Objective-C
    - (void)setupCoreKitQChat:(NIMSDKOption * _Nonnull)option;
    
  • 参数说明

    NIMSDKOption 参数 是否必传 说明
    appKey 云信控制台获取到的 App Key
    apnsCername APNs 推送证书名,如不需要实现离线推送可不配置
    pkCername PushKit 推送证书名,如不需要实现离线推送可不配置
  • 示例代码:

    Swift
    swiftlet option = NIMSDKOption()
    option.appKey = "your app key"
    option.apnsCername = "云信控制台配置的 APNS 推送证书名称"
    option.pkCername = "云信控制台配置的 PushKit 推送证书名称"
    QChatKitClient.instance.setupCoreKitQChat(option)
    
    // 初始化@功能(可选)
    let _ = NEAtMessageManager.instance
    
    Objective-C
    NIMSDKOption *option = [NIMSDKOption optionWithAppKey:AppKey];
    option.apnsCername = @"";
    option.pkCername = @"";
    [[QChatKitClient instance] setupCoreKitQChat:option];
    
    // 初始化@功能(可选)
    NEAtMessageManager * _ = [NEAtMessageManager instance];
    

登录

在完成初始化后,调用 loginQChat 方法登录 QChat。

  • 方法原型

    Swift
    /// 圈组登录(同步登录IM)
    /// - Parameters:
    ///   - loginParam: 登录参数
    ///   - completion: 回调
    public func loginQChat(_ loginParam: QChatLoginParam,
                           completion: @escaping (Error?, QChatLoginResult?) -> Void)
    
    Objective-C
    /// 圈组登录(同步登录IM)
    /// - Parameters:
    ///   - loginParam: 登录参数
    ///   - completion: 回调
    
    - (void)loginQChat:(QChatLoginParam *)loginParam
          completion:(void (^)(NSError *error, QChatLoginResult *result))completion;
    
  • 参数说明

    参数 类型 说明
    account String 云信 IM 账号(accid)
    token String 云信 IM 账号对应的 token
    block (Error?) -> Void 异步回调
  • 示例代码

    Swift
              weak var weakSelf = self
              let param = QChatLoginParam(account, token)
              
              QChatKitClient.instance.loginQChat(param) { error, result in
                  if let err = error {
                      print("qchatLogin failed, error : ", err)
                  }else {
                      //在登录成功回调中初始化路由以及配置各个模块首页
                      /*
                      weakSelf?.initConfig()
                      weakSelf?.initializePage()
                      */
                  }
              }
    
    Objective-C
    __weak typeof(self) weakSelf = self;
    QChatLoginParam *param = [[QChatLoginParam alloc] initWithAccount:account token:token];
    
    [[QChatKitClient instance] loginQChat:param completion:^(NSError *error, id result) {
      if (error) {
          NSLog(@"qchatLogin failed, error: %@", error);
      } else {
          // 在登录成功回调中初始化路由以及配置各个模块首页
          /*
          [weakSelf initConfig];
          [weakSelf initializePage];
          */
      }
    }];
    

后续步骤

为保障通信安全,如果您在调试环境中的使用的是云信控制台生成的 IM 账号(测试用),请确保在后续的正式生产环境中,将其替换为通过 IM 服务端 API 生成的正式 IM 账号。

此文档是否对你有帮助?
有帮助
去反馈
  • 前提条件
  • 初始化
  • 登录
  • 后续步骤