框架接入

更新时间:2024-10-28 08:25:56下载pdf

本文介绍智能生活 App SDK iOS 版 UI 业务包通用配置说明。更多详情,访问 GitHub 项目 tuya-bizbundle-ios-sdk-sample-objc

业务包组件

Cocoapods 组件 说明 是否必选
ThingSmartBizCore 业务基础处理库,用于启动业务包以及自定义一些功能 必选
ThingModuleServices 业务包模块实现的协议 必选
ThingSmartXXXBizBundle 各种业务包实现组件 可选

ThingSmartBizCoreThingModuleServices 是使用业务包必须要依赖的基础库。

核心库(ThingSmartBizCore)

提供启动业务包以及自定义配置功能。

主题色及自定义配置功能

该部分的配置,需要客户按照如下方式生成一份名为 thing_custom_config.json 的文件放入工程目录下:

{
    "config": {
        "appId": 123,
        "thingAppKey": "xxxxxxxxxxxx",
        "appScheme": "ThingSmart",
        "hotspotPrefixs": ["SmartLife"],
        "needBle": true
    },
   "colors":{
        "themeColor": "#FF5A28",
    }
}

参数介绍

参数 说明 类型 必选 默认值
appId 应用 ID,在涂鸦开发者平台进入您的应用/SDK管理页面。应用页面 URL 中的 ID 参数即为 appId,例如链接为 https://platform.tuya.com/oem/app?id=888888,则 appId888888 Number
thingAppKey 涂鸦开发者平台 中对应 SDK 中的 AppKey String
appScheme 涂鸦开发者平台 中对应 SDK 中的 渠道标识符 String
hotspotPrefixs 配网设备热点前缀 Array [“SmartLife”]
needBle 是否需要支持蓝牙设备配网 Boolean true
themeColor UI 主题色设置 String #FF5A28

接口介绍

业务包基础库,提供业务包调用的入口方法,同时也提供了您需要实现协议服务的注册方法。具体提供的方法如下:

/**
 * Get the instance that implements the special service protocol
 * eg:
 *  id<ThingMallProtocol> impl = [[ThingSmartBizCore sharedInstance] serviceOfProtocol:@protocol(ThingMallProtocol)];
 *  [impl xxx]; // your staff...
 *
 * @param serviceProtocol service protocol
 * @return instance
 */
- (id)serviceOfProtocol:(Protocol *)service;

/**
 * Register an instance for a service that cannot be served by BizBundle itself
 * Each service can only register one instance or class at a time, whichever is the last
 *
 * @param service   service protocol
 * @param instance  instance which conforms to the service protocol, strong reference
 *                  unregister if nil
 */
- (void)registerService:(Protocol *)service withInstance:(id)instance;

/**
 * Register a class for service that cannot be served by BizBundle itself
 * Each service can only register one instance or class at a time, whichever is the last
 *
 *
 * @param service   service protocol
 * @param cls       class which conforms to the service protocol, [cls new] to get instance
 *                  unregister if nil
 */
- (void)registerService:(Protocol *)service withClass:(Class)cls;

/**
 * Register route handler for the route that cannot be handled by BizBundle itself
 * @param handler   block to handle route
 *                  @param url  the route url
 *                  @param raw  the route raw data
 *                  @return true if the route can be handled, otherwise return false
 */
- (void)registerRouteWithHandler:(BOOL(^)(NSString *url, NSDictionary *raw))handler;

/**
* Update config of biz resource
*
*/
- (void)updateConfig;

账号登录成功后,务必调用 - (void)updateConfig 接口更新必要的缓存数据,否则部分业务包将无法正常使用。

除设备控制 UI 业务包外,其余业务包均通过 push 方式进行内部跳转。因此,请确保您的工程已设置了 RootViewController

服务协议(ThingModuleServices)

提供各个业务包实现的服务协议。

集成业务包

使用 CocoaPods 集成

Podfile 文件中添加以下内容,完成业务包核心库添加:

source 'https://github.com/tuya/tuya-pod-specs.git'
source 'https://cdn.cocoapods.org/'
platform :ios, '11.0'

target 'your_target_name' do
    # 添加涂鸦智能生活 App SDK
    pod "ThingSmartHomeKit"
    # 若需要相关业务包功能,请添加相关业务库
    pod "ThingSmartXXXBizBundle"
end

然后在项目根目录下执行 pod update 命令,集成第三方库。有关 CocoaPods 的使用,请参考 CocoaPods Guide

自定义配置项

填写业务包初始化需要的配置。

{
    "config": {
        "appId": 123,
        "thingAppKey": "xxxxxxxxxxxx",
        "appScheme": "ThingSmart"
    },
    "colors":{
        "themeColor": "#FF5A28",
    }
}

生成名为 thing_custom_config.json 的 json 文件,放到工程根目录下。

涂鸦业务包准备工作已经完成,可以开始业务包的使用。具体业务包的集成使用,请参考对应的业务包文档。

多语言

带 UI 业务包的多语言将会根据您的工程所支持的多语言来显示相对应的语言。如果您的工程不做多语言配置,则业务包 UI 以英文为默认语言显示。

通过调用以下接口,可以切换多语言、替换语言翻译。

/// Switch the app language, if the 'language' is passed as nil, then use the system language.
/// @param language Use the key in the returned information from the 'supportedLanguages' method.
- (void)languageSwitchTo:(ThingSmartBizLanguageKey _Nullable)language;

/**
 Returns a dictionary of supported languages with complete translations.

 This method provides a list of languages that have full translation support. Each entry in the dictionary
 maps a language code to its corresponding language name.

 @return A dictionary where keys are language codes (e.g., "en", "fr") and values are the full names of the languages.
    key:language IDs for all the localizations contained in the bundle. e.g. 'en'
    value:language name
 */
- (NSDictionary<ThingSmartBizLanguageKey, NSString *> *_Nonnull)recommendedLanguages;

/**
 Returns the current language key being used by the application.

 This method provides the language key that represents the current language setting of the application.
 It reflects the language that the app is currently using for its interface and content. This can be
 useful for determining the current localization and for displaying language-specific content.
 
 @return The 'ThingSmartBizLanguageKey' indicates the current language settings of the app.
 */
- (ThingSmartBizLanguageKey)currentLanguageKey;

/**
 Sets a language interceptor to customize language string replacements.

 This method allows you to set a language interceptor that conforms to the 'ThingSmartBizLanguageInterceptorProtocol'.
 The interceptor is used to handle custom language string replacements within the application.

 @param interceptor An object conforming to 'ThingSmartBizLanguageInterceptorProtocol' to handle language string replacements.
 
 @throws NSException Throws a ThingSmartBizCoreExceptionName if the interceptor is nil or if the [ThingLanguageLoader sharedInstance].delegate is already set.
 */
- (void)setLanuageInterceptor:(id<ThingSmartBizLanguageInterceptorProtocol>)interceptor;

示例

// 查看支持切换的语言
NSDictionary *dic = [[ThingSmartBizCore sharedInstance] recommendedLanguages];
// 切换到指定语言
[[ThingSmartBizCore sharedInstance] languageSwitchTo:ThingSmartBizLanguageKeyJapanese];
// 获取当前语言
ThingSmartBizLanguageKey lang = [[ThingSmartBizCore sharedInstance] currentLanguageKey]
// 替换语言翻译
[[ThingSmartBizCore sharedInstance]  setLanuageInterceptor: xxxx];

当 iOS App 切换语言之后,你需要重新调用 [ThingSmartSDK sharedInstance].deviceToken = deviceToken; 方法。这将通知云端进行语言切换,包括云端推送的 Push 消息。特别地,通常建议在系统的回调方法 application:didRegisterForRemoteNotificationsWithDeviceToken: 中设置该值,示例如下:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [ThingSmartSDK sharedInstance].deviceToken = deviceToken;
}

这确保无论何时 App 的语言发生改变,后台都会立即接收到更新。这是一个重要的步骤,能确保你的 App 能为全球用户提供稳定的、个性化的体验。