跳到主要内容

SDK初始化

更新时间:2025-04-01 16:44:30

SDK初始化

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

// 导入初始化SDK头文件
#import <RTCXIotSmart/RTCXIotSmart.h>
// 导入日志SDK头文件
#import <RTCXLog/RTCXLog.h>
// sdk配置项
@interface RTCXIotSmartConfig : NSObject
@property (nonatomic, copy) NSString* appKey;// 平台分配的AppKey
@property (nonatomic, copy) NSString* appSecret;// 平台分配的AppSecret
@end

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

@return version 字符串
*/
+ (nonnull NSString *)getCurrentSDKVersion;
/**
SDK初始化是否完成

@return 是否完成 YES: 完成 NO:未完成
*/
- (BOOL)sdkInitializationCompleted;
@end

初始化 RTCXIotSmart 单例,设置 App Key 和 App Secret,调用示例如下:

#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.appKey = appKey;//需要自行配置自有app的appkey
config.appSecret = appSecret;//需要自行配置自有app的appSecurit
[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] setSdkInitCompletedCallback:^{
[weakSelf autoLoginCheck];
}];