跳到主要内容

SDK初始化

更新时间:2025-07-04 11:30:46

SDK初始化

专属版SDK初始化同【通用版-初始化】类似,只是RTCXIotSmartConfig初始化对象入参请使用privAppKey和privAppSecret

建议在 AppDelegate 类中对 SDK 进行初始化操作,只有SDK初始化完成以后才可以调用SDK相关接口,否则会出现接口调用异常。

// 导入初始化SDK头文件
#import <RTCXIotSmart/RTCXIotSmart.h>
// 导入日志SDK头文件
#import <RTCXLog/RTCXLog.h>
/// 私有化版本SDK配置项
@interface RTCXIotSmartConfig (privatization)

@property (nonatomic, copy) NSString* _Nullable privAppKey;
@property (nonatomic, copy) NSString* _Nullable privAppSecret;
// 私有化regionId
@property (nonatomic, copy) NSNumber* _Nullable privRegionId;
@end

typedef void (^ _Nullable RTCXPRIVSDKINITFINISHEDBLOCK)(void);
@interface RTCXIotSmart (privatization)
/**
私有化SDK初始化完成Block回调
*/
@property (nonatomic, copy) RTCXPRIVSDKINITFINISHEDBLOCK privSdkInitFinishedCallback;
/**
私有化SDK初始化是否完成

@return 是否完成 YES: 完成 NO:未完成
*/
@property (nonatomic, assign) BOOL privInitFinished;
@end

@interface RTCXIotSmart : NSObject
/// SDK运行时的配置 ,请在调用 sdk API [application:didFinishLaunchingWithOptions:] 前调用此方法完成初始化配置,参见`RTCXIotSmartConfig`。
@property (nonatomic, strong, nullable) RTCXIotSmartConfig * config;
/// 单例方法
+ (instancetype _Nonnull )sharedInstance;
/**
是否打开调试开关,打开时会开启底层sdk的log, 需要排查bug时,建议打开
@param turnOndebug YES表示打开Log,No:表示关闭
*/
- (void)setDebug:(bool)turnOndebug;
/**
@remark 获取当前SDK版本

@return version 字符串
*/
+ (nonnull NSString *)getCurrentSDKVersion;

@end

初始化 RTCXIotSmart 单例,设置 privAppKey 和 privAppSecret,调用示例如下:

#import "AppDelegate.h"
// 导入初始化SDK头文件
#import <RTCXIotSmart/RTCXIotSmart.h>
// 导入日志SDK头文件
#import <RTCXLog/RTCXLog.h>

@interface AppDelegate ()

@end

@implementation AppDelegate

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

// 创建并设置窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];

#if defined(DEBUG)
// 开启控制台日志
[RTCXLog showInConsole:YES];
#else
// 开启控制台日志
[RTCXLog showInConsole:YES];
// 日志重定向
[self redirectLocalLogManager];
#endif
NSString *appKey = APP_KEY;
NSString *appSecret = APP_SECRET;
NSArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:IOT_APP_KEY_APP_SECRET];
if (array.count == 2) {
appKey = array.firstObject;
appSecret = array.lastObject;
}
// 用户鉴权失效监听
[RTCXService(RTCXOpenAccountProtocol) setAuthenticationAbnormalCallback:^(NSInteger code, NSString * _Nonnull message) {
if (code == 30314) {// token失效
if ([RTCXService(RTCXOpenAccountProtocol) isLogin]) {
[[self currentViewController].navigationController popToRootViewControllerAnimated:YES];// token过期退出登录
}
}
}];
// 初始化SDK
RTCXIotSmartConfig *config = [RTCXIotSmartConfig new];
config.privAppKey = appKey;//需要自行配置自有app的appkey
config.privAppSecret = appSecret;//需要自行配置自有app的appSecurit
//config.regionType = REGION_ALL;//设置App使用范围
[RTCXIotSmart sharedInstance].config = config;

// 注册自定义的鉴权处理对象
// RTCXIoTAuthenticationTest *test = [[RTCXIoTAuthenticationTest alloc] initWithCredentialManager:RTCXCredentialManager.sharedManager];
// [RTCXRequestClient registerDelegate:test forAuthenticationType:RTCXAuthenticationTypeIoT];
LoginViewController *loginVC = [[LoginViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVC];
self.window.rootViewController = nav;
// 显示窗口
[self.window makeKeyWindow];
[self.window makeKeyAndVisible];
[RTCXPushService(RTCXPushProtocol) application:application didFinishLaunchingWithOptions:launchOptions];
[self registerRemoteNotifications];// 注册推送通知
return YES;
}
@end

专属版SDK初始化完成回调

[[RTCXIotSmart sharedInstance] setPrivSdkInitFinishedCallback:^{

}];

检查SDK是否初始化完成:

BOOL finished = [[RTCXIotSmart sharedInstance] privInitFinished];