SDK initialization

更新时间:
复制 MD 格式

After you download and integrate the SDKs for your app, you must initialize them.

Overview

For SDKs at API Level 8 or later, you do not need to initialize each SDK individually. You can use a unified interface to initialize all required SDKs at once. If you use an SDK at API Level 7 or earlier, you must upgrade to the latest version, API Level 10. For more information, see SDK upgrade.

The unified initialization interface initializes the following SDKs based on the SDK configuration items that you select when you download the SDK.

  • API channel SDK (Required for initialization)

  • Account and user SDK (Required for initialization)

  • Identity authentication SDK (Required for initialization)

  • Persistent connection channel SDK

  • SDK for the Thing Specification Language model

  • Mobile application push SDK

  • BoneMobile container SDK

  • Link Visual video media SDK

SDK initialization

  1. Download and integrate the SDK. For more information, see Download and integrate the SDK.

  2. Initialize the SDK.

    1. Configure the SDK.

      #import <IMSIotSmart/IMSIotSmart.h>
      
      IMSIotSmartConfig *config = [IMSIotSmartConfig new];
      config.regionType = REGION_ALL; // For the value range, see the enumeration type `IMSRegionType`.
      // These are the default configurations. You can adjust them as needed.
      
      /* New in this version
      1. appKey, appSecurity, and setSigner
       1.1 If you set appKey and appSecurity, you do not need to call setSigner. The SDK will not perform signing or encryption.
       1.2 If you do not set appKey and appSecurity, you must call setSigner. The signer must implement the signing method in the IMSOpenSecurityGuardSigner protocol.
      2. To use the push feature in the new SDK version, you must set appKey and appSecurity. If you do not use the push feature, see point 1.
      
      */
      // Set appKey and appSecurity.
      config.appKey = @"xxxxxxx"; // Configure the appKey of your app.
      config.appSecurity = @"xxxxxxx"; // Configure the appSecurity of your app.
      
      [IMSIotSmart sharedInstance].config = config;
      
      // Set the signer.
      id<IMSOpenSecurityGuardSigner> signer = [Class new];
      [[IMSIotSmart sharedInstance] setSigner:signer];
      Note

      If you use the default configurations, you can skip this step.

    2. IMSOpenSecurityGuardSigner Protocol

      // Description
      /// - Parameters:
      ///   - text: The string to be encrypted.
      ///   - type: The encryption type, such as hmacSha1 or hmacSha256.
      ///	  -useBase64: Specifies whether to perform Base64 encoding.
      -(NSString*)signWithText:(NSString*)text AndType:(IMSOpenSecuritySignType)type useBase64:(BOOL)useBase64;
    3. Start the initialization.

      #import <IMSIotSmart/IMSIotSmart.h>
      
      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      
          IMSIotSmartConfig *config = [IMSIotSmartConfig new];
          config.regionType = REGION_ALL;  // For the value range, see the enumeration type `IMSRegionType`.
          
          // Set appKey and appSecurity.
          config.appKey = @"xxxxxxx"; // Configure the appKey of your app.
          config.appSecurity = @"xxxxxxx"; // Configure the appSecurity of your app.
          
          [IMSIotSmart sharedInstance].config = config;
      
          // Set the signer.
          id<IMSOpenSecurityGuardSigner> signer = [Class new];
          [[IMSIotSmart sharedInstance] setSigner:signer]
      
          [[IMSIotSmart sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
      
          return YES;
      }
      
      - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
         // If your app has not integrated the mobile application push feature, do not make this call.
          [[IMSIotSmart sharedInstance] application:application didReceiveRemoteNotification:userInfo];
      }
      
      - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
          // If your app has not integrated the mobile application push feature, do not make this call.
          [[IMSIotSmart sharedInstance] application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
      }
      
      - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
          // If your app has not integrated the mobile application push feature, do not make this call.
          [[IMSIotSmart sharedInstance] application:application didFailToRegisterForRemoteNotificationsWithError:error];
      }
    4. Set the country.

      IoT Platform cloud services are deployed across multiple regions. Whether you need to set the country during initialization depends on your SDK version and the value of the config.regionType parameter.

      Parameter value

      API Level 8 and earlier

      API Level 9 and later

      REGION_CHINA_ONLY

      Not required

      Not required

      REGION_ALL

      Required

      Note

      If you do not set the country, the SDK initialization process is paused, and you cannot use any SDK APIs. To set the country, see Archived Documents.

      Not required (The country must be set when you register the app account).

  3. Set the list of products that the app can use for network configuration.

    #import <IMSIotSmart/IMSIotSmart+scope.h>
    [[IMSIotSmart sharedInstance] configProductScope:PRODUCT_SCOPE_ALL];
        /// Configure the scope of products visible in the app. PRODUCT_SCOPE_ALL: indicates all published and unpublished products in the current project. PRODUCT_SCOPE_PUBLISHED: indicates only published products.
        /// For a formally released app, select PRODUCT_SCOPE_PUBLISHED.

SDK API Reference

When you use the SDK provided by IoT Platform, see the SDK API Reference for information about the API comments.