Device Control UI BizBundle

Last Updated on : 2024-11-18 11:32:43

Feature overview

Device Control UI BizBundle for iOS is the core container of the control panel for each smart device within Tuya’s ecosystem. Based on Tuya Smart Life App SDK for iOS, the UI BizBundle encapsulates the APIs to load and control panels and accelerates the application development process. The following features are supported:

  • Panel loading: supports devices of multiple protocols, such as Wi-Fi, Zigbee, Bluetooth mesh, and Bluetooth Low Energy (LE).
  • Device control: controls a single device and device groups. Device groups cannot be managed.
  • Scheduled tasks: configures scheduled tasks for specific devices.

Integrate with the UI BizBundle

Add the components of the Device Control UI BizBundle to the Podfile and run the command pod update.

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

target 'your_target_name' do
  # TuyaSmart SDK
  pod "TuyaSmartHomeKit"
  # Adds the UI BizBundle.
  pod 'TuyaSmartPanelBizBundle'
  # To implement IP camera (IPC) features, add the dependencies on certain IPC plug-ins.
  # pod 'TuyaSmartCameraPanelBizBundle'
  # pod 'TuyaSmartCameraRNPanelBizBundle'

  # Depends on the health and fitness plug-ins to integrate with their capabilities if needed. Note that you must declare the permissions for the app to access the health and fitness capabilities. Otherwise, the request for launching the app on the App Store might be rejected.
  # pod 'TuyaRNApi/Health'
  # pod 'TuyaRNApi/HealthCenter'

  # Depends on the smart speaker plug-ins to integrate with their features if needed.
  # pod 'TuyaRNApi/Speak'

  # Depends on the home plug-ins provided by mini programs to integrate with their features if needed.
  # pod 'TYCBTHomeKit'
end
  • The UI BizBundle includes the open source React Native framework (v0.59.11).
    1. If your iOS project does not integrate with React Native, follow the procedure in this topic to integrate with the UI BizBundle.
    2. If your iOS project has integrated with React Native, perform the following steps to check whether the UI BizBundle can be integrated:
      1. If React Native has been integrated and its version is later than v0.59.11, the UI BizBundle cannot be integrated due to version incompatibility.
      2. If React Native has been integrated and its version is later than v0.51 and equal to or earlier than v0.59.11, access to the Tuya panels might be failed due to the repeated uses of the React Native library. Use either of the following methods to troubleshoot the problem:
        1. Remove React Native from your iOS project and use React Native v0.59.11 that the UI BizBundle depends on.
        2. Rename React Native that is integrated with your iOS project to avoid the repeated use of the React Native library.
          If both methods failed, the UI BizBundle cannot be integrated.

The UI BizBundle encapsulates a series of React Native API methods for panels. This requires specific declarations of privacy and permissions from Apple.

  • If a device panel supports album features such as the cloud album, add the following permission declaration to info.plist of your project:

    NSPhotoLibraryAddUsageDescription
    
  • If a device panel supports camera features such as the cloud album, add the following permission declaration to info.plist of your project:

    NSCameraUsageDescription
    
  • If a device panel supports location features, add the following permission declaration to info.plist of your project:

    NSLocationWhenInUseUsageDescription
    
  • If a device panel supports microphone features such as the music light panel, add the following permission declaration to info.plist of your project:

    NSMicrophoneUsageDescription
    
  • If a device panel supports Bluetooth features, add the following permission declaration to info.plist of your project:

    NSBluetoothAlwaysUsageDescription
    NSBluetoothPeripheralUsageDescription
    

Service protocols

Provide services

The UI BizBundle relies on the implementation of the protocol TYPanelProtocol to provide services. You can view the TYPanelProtocol.h file in the TYModuleServices component.

@protocol TYPanelProtocol <NSObject>
NS_ASSUME_NONNULL_BEGIN

// Clears cached data of the panel.
- (void)cleanPanelCache;


/**
 * Returns the device panel controller.
 *
 * @param deviceModel The device model.
 * @param initialProps  The custom initialization parameter to be set in `initialProps` of the RN application with the key 'extraInfo'.
 * @param contextProps  The context of the custom panel to be set in `Panel Context` with the key 'extraInfo'.
 * @param completionHandler The callback for the view controller.
 */
- (void)getPanelViewControllerWithDeviceModel:(TuyaSmartDeviceModel *)deviceModel
                                 initialProps:(nullable NSDictionary *)initialProps
                                 contextProps:(nullable NSDictionary *)contextProps
                            completionHandler:(void (^ _Nullable)(__kindof UIViewController * _Nullable panelViewController, NSError * _Nullable error))completionHandler;

/**
 * Returns the group panel controller.
 *
 * @param groupModel The group model.
 * @param initialProps  The custom initialization parameter to be set in `initialProps` of the RN application with the key 'extraInfo'.
 * @param contextProps  The context of the custom panel to be set in `Panel Context` with the key 'extraInfo'.
 * @param completionHandler The callback for the view controller.
 */
- (void)getPanelViewControllerWithGroupModel:(TuyaSmartGroupModel *)groupModel
                                initialProps:(nullable NSDictionary *)initialProps
                                contextProps:(nullable NSDictionary *)contextProps
                           completionHandler:(void (^ _Nullable)(__kindof UIViewController * _Nullable panelViewController, NSError * _Nullable error))completionHandler;

// The RN version number.
- (NSString *_Nonnull)rnVersionForApp;

NS_ASSUME_NONNULL_END
@end

Depend on services

The UI BizBundle is used to load device panels and provide varied features depending on different devices. To ensure the normal use of these features, the UI BizBundle depends on the following protocols: TYFamilyProtocol, TYDeviceDetailProtocol, TYSettingsProtocol, TYOTAGeneralProtocol, TYGroupHandleProtocol, TYRNCameraProtocol, and TYCameraProtocol.

TYFamilyProtocol

Provides current home information required by the UI BizBundle. This protocol must be implemented.

/**
 Returns the current home. If the current user does not have a home, `nil` is returned.
 
 @return TuyaSmartHome
 */
- (TuyaSmartHome *)getCurrentHome;

TYDeviceDetailProtocol

Implements the activity of tapping and navigation in the top right corner of the device panel.

/**
 Tap the button in the top right corner of the device panel to navigate to the device details page.

 @param device The device.
 @param group The group if any. It is optional.
 */
- (void)gotoDeviceDetailDetailViewControllerWithDevice:(TuyaSmartDeviceModel *)device group:(TuyaSmartGroupModel *)group;

TYSettingsProtocol

Provides the optional switch to enable a certain sound effect on the app when device control DPs are sent. The following API method is implemented to return whether the sound effect is enabled:

/**
 * Indicates whether the sound effect is enabled.
 */
- (BOOL)soundEnabled;

TYOTAGeneralProtocol

Enters the device panel and checks for device firmware updates. The following API method is implemented to check for device firmware updates:

/**
 Checks for device firmware updates, and if any, shows the update prompt.
 
 @param deviceModel The device that checks for firmware updates.
 @param isManual Specifies whether to enable manual checks for firmware updates.
  @param theme The theme color.
 `YES`: Manually check for updates. A loading dialog box appears during the check. If a new update is found (check for updates, forced updates, and update notifications), OTA VC is displayed.
 `NO`: Automatically check for updates. A loading dialog box does not appear during the check. When forced updates and update notifications are enabled, the firmware update prompt appears. Click **OK** to show OTA VC.
 */
- (void)checkFirmwareUpgrade:(TuyaSmartDeviceModel *)deviceModel isManual:(BOOL)isManual theme:(TYOTAControllerTheme)theme;

TYGroupHandleProtocol

Implements the activity of navigation to a mesh group page on the panel for a Bluetooth LE mesh device. The following API method is implemented:

/**
Navigates to a local mesh group.

@available 1.0.0
@param params 
@param success 
@param failure 
*/
- (void)impl_jumpToMeshLocalGroup:(NSDictionary*)params success:(RCTResponseSenderBlock)success failure:(RCTResponseErrorBlock)failure ;

TYRNCameraProtocol

Navigates to and loads the RN panel for an IPC. If the BizBundle TuyaSmartCameraRNPanelBizBundle is integrated, this protocol is not required.

/**
 Returns the IPC RN panel.
 @param devId The device ID of the IPC.
 */
- (UIViewController *)cameraRNPanelViewControllerWithDeviceId:(NSString *)devId;

TYCameraProtocol

Navigates to and loads the native panel for an IPC. If the BizBundle TuyaSmartCameraPanelBizBundle is integrated, this protocol is not required.

/**
 Returns the IPC native panel.
 @param devId The device ID of the IPC.
 @param uiName The UI name of the IPC. The value varies depending on different panel versions.
 */
- (UIViewController *)viewControllerWithDeviceId:(NSString *)devId uiName:(NSString *)uiName;

Usage instruction

Things to note

  1. Before the call of any API method, make sure that the user has logged in to the app.

  2. Before the UI BizBundle is used, the getCurrentHome method provided by the protocol TYFamilyProtocol must be implemented first.

ObjC:

#import <TuyaSmartBizCore/TuyaSmartBizCore.h>
#import <TYModuleServices/TYFamilyProtocol.h>

- (void)initCurrentHome {
    // Registers the protocol to be implemented.
    [[TuyaSmartBizCore sharedInstance] registerService:@protocol(TYFamilyProtocol) withInstance:self];
}

// Implements the protocol method.
- (TuyaSmartHome *)getCurrentHome {
    TuyaSmartHome *home = [TuyaSmartHome homeWithHomeId:@"Current home ID"];
    return home;
}

Swift:

import TuyaSmartDeviceKit

class TYActivatorTest: NSObject,TYFamilyProtocol{
  	
    func test() {
        TuyaSmartBizCore.sharedInstance().registerService(TYFamilyProtocol.self, withInstance: self)
    }
    
    func getCurrentHome() -> TuyaSmartHome! {
        let home = TuyaSmartHome.init(homeId: 111)
        return home
    }
    
}

Clear cached data of the panel

ObjC:

id<TYPanelProtocol> impl = [[TuyaSmartBizCore sharedInstance] serviceOfProtocol:@protocol(TYPanelProtocol)];
[impl cleanPanelCache];

Swift:

let impl = TuyaSmartBizCore.sharedInstance().service(of: TYPanelProtocol.self) as? TYPanelProtocol
impl?.cleanPanelCache()

Open a device panel

ObjC:

id<TYPanelProtocol> impl = [[TuyaSmartBizCore sharedInstance] serviceOfProtocol:@protocol(TYPanelProtocol)];
// Returns the panel view controller and enables automatic navigation.
if (deviceModel) {
    [impl getPanelViewControllerWithDeviceModel:deviceModel initialProps:nil contextProps:nil completionHandler:^(__kindof UIViewController * _Nullable panelViewController, NSError * _Nullable error) {
    }];
} else if (groupModel) {
    [impl getPanelViewControllerWithGroupModel:groupModel initialProps:nil contextProps:nil completionHandler:^(__kindof UIViewController * _Nullable panelViewController, NSError * _Nullable error) {
    }];
}

Swift:

let impl = TuyaSmartBizCore.sharedInstance().service(of: TYPanelProtocol.self) as? TYPanelProtocol
// Returns the panel view controller and enables automatic navigation.
impl?.getPanelViewController(with: deviceModel, initialProps: nil, contextProps: nil, completionHandler: { (vc, err) in

})
impl?.getPanelViewController(with: groupModel, initialProps: nil, contextProps: nil, completionHandler: { (vc, err) in

})