OTA UI BizBundle

Last Updated on : 2023-07-13 07:14:38download

An over-the-air (OTA) update enables automatic firmware download and updates over the network. OTA UI BizBundle helps you quickly integrate with firmware update capabilities.

Integrate with the UI BizBundle

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

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

target 'your_target_name' do
  # Adds the UI BizBundle.
  pod 'ThingSmartOTABizBundle'
end

To support OTA updates for Bluetooth devices, add the following permission declaration to info.plist of your project:

NSBluetoothAlwaysUsageDescription
NSBluetoothPeripheralUsageDescription

Service protocols

ThingOTAGeneralProtocol

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

#import <Foundation/Foundation.h>

@class ThingSmartDeviceModel;

typedef NS_ENUM(NSUInteger, ThingOTAControllerTheme) {
    ThingOTAControllerWhiteTheme,
    ThingOTAControllerBlackTheme
};

@protocol ThingOTAGeneralProtocol <NSObject>

/**
 Checks whether the current device supports OTA firmware updates.

 @param deviceModel The device that checks for firmware updates.
 `YES`: supported
 `NO`: not supported
 */
- (BOOL)isSupportUpgrade:(ThingSmartDeviceModel *)deviceModel;

/**
 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 checking of 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:(ThingSmartDeviceModel *)deviceModel isManual:(BOOL)isManual theme:(ThingOTAControllerTheme)theme;

@end

ThingOTAGeneralExternalProtocol

To customize the callback that is executed after the forced update is canceled (default logic is implemented by popToRootViewController), you must implement the protocol method provided by ThingOTAGeneralExternalProtocol.

@protocol ThingOTAGeneralExternalProtocol <NSObject>

@optional
/// @brief Customizes the callback that is executed when a forced update is canceled.
///
/// 1. During a forced update, tap Cancel in the dialog box to cancel the update.
/// 2. During a forced update, tap Back of `navigationBar`. If the update progress dialog box appears, tap OK.
///
/// @return `YES`: Custom callback. `NO`: default logic. `[self.navigationController popToRootViewControllerAnimated:YES]`
- (BOOL)didTapCancelForceUpgrade;

@end

Usage instruction

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

Check for update and access the firmware update page

ObjC:

#import <ThingModuleServices/ThingModuleServices.h>
#import <ThingSmartBizCore/ThingSmartBizCore.h>

+ (void)test:(ThingSmartDeviceModel *)device {
    id<ThingOTAGeneralProtocol> otaImp = [[ThingSmartBizCore sharedInstance] serviceOfProtocol:@protocol(ThingOTAGeneralProtocol)];

    if ([otaImp isSupportUpgrade:device]) {
        [otaImp checkFirmwareUpgrade:device isManual:YES theme:ThingOTAControllerWhiteTheme];
    } else {
        NSLog(@"unsupported");
    }
}

Swift:

func test(_ deviceModel: ThingSmartDeviceModel) {
	guard let otaImp = ThingSmartBizCore.sharedInstance().service(of: ThingOTAGeneralProtocol.self) as? ThingOTAGeneralProtocol else { return }

	if otaImp.isSupportUpgrade(deviceModel) {
		otaImp.checkFirmwareUpgrade(deviceModel, isManual: true, theme: .whiteTheme)
	} else {
		print("unsupported")
	}
}

Customize the callback for canceling forced update

After a forced update is canceled, [self.navigationController popToRootViewControllerAnimated:YES] is called by default. To customize the callback, you must register and implement ThingOTAGeneralExternalProtocol.

Objective-C: Register [ThingOTAObjcDemo registerService]; when needed.

#import <ThingModuleServices/ThingModuleServices.h>
#import <ThingSmartBizCore/ThingSmartBizCore.h>

@interface ThingOTAObjcDemo () <ThingOTAGeneralExternalProtocol>

@end
@implementation ThingOTAObjcDemo

/// Registers the protocol.
+ (void)registerService {
    [[ThingSmartBizCore sharedInstance] registerService:@protocol(ThingOTAGeneralExternalProtocol) withInstance:self];
}

/// Implements the protocol.
#pragma mark - ThingOTAGeneralExternalProtocol -
- (BOOL)didTapCancelForceUpgrade {
    NSLog(@"didTapCancelForceUpgrade");
    return YES;
}

@end

Swift: Register OTASwiftDemo.registerService() when needed.

import Foundation

class OTASwiftDemo: NSObject, ThingOTAGeneralExternalProtocol {
    func registerService() {
        ThingSmartBizCore.sharedInstance().registerService(ThingOTAGeneralExternalProtocol.self, withInstance: self)
    }

    // MARK: - ThingOTAGeneralExternalProtocol -
    func didTapCancelForceUpgrade() -> Bool {
        print("didTapCancelForceUpgrade")
        return true
    }
}