Fast Integration with Smart Protect App SDK for iOS

Last Updated on : 2022-03-03 07:00:58download

This topic describes how to use CocoaPods to quickly integrate Tuya Smart Protect App SDK for iOS into your project. The SDK requires iOS 9.0 or later.

Integrate with the SDK

Use CocoaPods for fast integration

  1. We recommend that you update CocoaPods to the latest version. For more information about CocoaPods, see CocoaPods Guides.

  2. Add the following code block to the Podfile:

    platform :ios, '9.0'
    
    target 'Your_Project_Name' do
    	pod "TuyaSecurityBaseKit"
    end
    
  3. In the root directory of your project, run pod update.

Initialize the SDK

  1. Open the project settings, click Target > General, and then modify Bundle Identifier to the iOS Bundle ID set on the Tuya IoT Development Platform.

  2. Import the security image to the root directory of the project, and rename it as t_s.bmp. Go to Project Settings > Target > Build Phases, and add this image to Copy Bundle Resources.

  3. Add the following content to the PrefixHeader.pch file:

    #import <TuyaSmartHomeKit/TuyaSmartKit.h>
    #import <TuyaSecurityBaseKit/TuyaSecurityBaseKit.h>
    

    Add the following content to the bridging header file xxx_Bridging-Header.h:

    #import <TuyaSmartHomeKit/TuyaSmartKit.h>
    #import <TuyaSecurityBaseKit/TuyaSecurityBaseKit.h>
    
  4. Open the AppDelegate.m file and initialize the SDK in [AppDelegate application:didFinishLaunchingWithOptions:].

Configure the SDK

- (void)startWithAppKey:(NSString *)appKey secretKey:(NSString *)secretKey;

Parameters

Parameter Description
appKey App key
secretKey AppSecret

Example

ObjC:

[[TuyaSmartSDK sharedInstance] startWithAppKey:<#your_app_key#> secretKey:<#your_secret_key#>];

Swift:

TuyaSmartSDK.sharedInstance()?.start(withAppKey: <#your_app_key#>, secretKey: <#your_secret_key#>)

Now, you are ready for app development.

Enable the debug mode

During the development, you can enable the debug mode and print logs for troubleshooting.

ObjC:

#ifdef DEBUG
    [[TuyaSmartSDK sharedInstance] setDebugMode:YES];
#else
#endif

Swift:

#if DEBUG
   TuyaSmartSDK.sharedInstance()?.debugMode = true
#else
#endif

Configure multilingual settings

The returned error messages and other UI text are displayed in languages as configured in the multilingual settings of your project and users’ mobile phone system languages.

To support a certain language, add it to Localization of your project.

Create a widget

  1. Perform the following steps:
  2. Modify the Podfile.
    post_install do |installer|
    	installer.pods_project.targets.each do |target|
    		target.build_configurations.each do |config|
    			config.build_settings['APPLICATION_EXTENSION_API_ONLY'] = 'NO'
    		end
    	end
    end
    
  3. Set AppGroups:
    1. Grant permissions on AppGroups.

      Only paid developer accounts can be granted permissions on AppGroups. Therefore, free developer accounts cannot be used to debug widget applications.

    2. Get the security image, AppKey, and AppSecret from the Tuya IoT Development Platform, and configure them to initialize the SDK. Before the SDK is initialized with AppKey, set APP_GROUP_NAME.
      [TuyaSmartSDK sharedInstance].appGroupId = APP_GROUP_NAME;
      [[TuyaSmartSDK sharedInstance] startWithAppKey:SDK_APPKEY secretKey:SDK_APPSECRET];
      

Use the SDK

  • Set homeId in the main project.
  • Use TuyaSmartDeviceModel.switchDp to check whether a device supports quick toggles.
  • Use viewWillAppear to check whether accounts and rooms are changed in the main project. If so, refresh the data.

Run the demo app

In the following example, a demo app is used to describe the process of app development with the App SDK. Before the development of your app, we recommend that you run the demo app.

Prepare the demo

In the Preparation topic, get the AppKey, AppSecret, and security image for iOS.

Make sure that BundleId, AppKey, AppSecret, and the security image are consistent with those used on the Tuya IoT Development Platform. Any mismatch will cause the SDK development app to be failed.

Demo features

The demo app is coded in Objective-C. You can get the sample in Objective-C on GitHub. The following features are supported:

  • User registration and login

  • User management

  • Home management

  • Wi-Fi Easy Connect (EZ) mode and access point (AP) mode

  • Device control

  • Protection capabilities

    Fast Integration with Smart Protect App SDK for iOS

FAQ

  • Problem: When an API request is made, an error message is returned in the following response:

    {
    	"success" : false,
    	"errorCode" : "SING_VALIDATE_FALED",
    	"status" : "error",
    	"errorMsg" : "Permission Verification Failed",
    	"t" : 1583208740059
    }
    
  • Solution: Make sure that BundleId, AppKey, AppSecret, and the security image are consistent with those used on the Tuya IoT Development Platform. Any mismatch will cause the authentication to be failed. For more information, see Preparation.