English
English
简体中文
Contact Us
Register
Log In
layoutIndex

OTA UI BizBundle

Last Updated on : 2022-09-16 09:22:41download

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-pod-specs"
source 'https://cdn.cocoapods.org/'
platform :ios, '11.0'

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

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

NSBluetoothAlwaysUsageDescription
NSBluetoothPeripheralUsageDescription

Service protocols

TYOTAGeneralProtocol

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

#import <Foundation/Foundation.h>

@class TuyaSmartDeviceModel;

typedef NS_ENUM(NSUInteger, TYOTAControllerTheme) {
    TYOTAControllerWhiteTheme,
    TYOTAControllerBlackTheme
};

@protocol TYOTAGeneralProtocol <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:(TuyaSmartDeviceModel *)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:(TuyaSmartDeviceModel *)deviceModel isManual:(BOOL)isManual theme:(TYOTAControllerTheme)theme;

@end

TYOTAGeneralExternalProtocol

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 TYOTAGeneralExternalProtocol.

@protocol TYOTAGeneralExternalProtocol <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 <TYModuleServices/TYModuleServices.h>
#import <TuyaSmartBizCore/TuyaSmartBizCore.h>

+ (void)test:(TuyaSmartDeviceModel *)device {
    id<TYOTAGeneralProtocol> otaImp = [[TuyaSmartBizCore sharedInstance] serviceOfProtocol:@protocol(TYOTAGeneralProtocol)];

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

Swift:

func test(_ deviceModel: TuyaSmartDeviceModel) {
	guard let otaImp = TuyaSmartBizCore.sharedInstance().service(of: TYOTAGeneralProtocol.self) as? TYOTAGeneralProtocol 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 TYOTAGeneralExternalProtocol.

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

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

@interface TYOTAObjcDemo () <TYOTAGeneralExternalProtocol>

@end
@implementation TYOTAObjcDemo

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

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

@end

Swift: Register OTASwiftDemo.registerService() when needed.

import Foundation

class OTASwiftDemo: NSObject, TYOTAGeneralExternalProtocol {
    func registerService() {
        TuyaSmartBizCore.sharedInstance().registerService(TYOTAGeneralExternalProtocol.self, withInstance: self)
    }

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