All Products
Search
Document Center

IoT Platform:Verify and connect a device

Last Updated:Jul 07, 2023

This topic describes how to initialize Link SDK for iOS to establish a connection between a device and IoT Platform.

Background information

Link SDK for iOS supports only the unique-certificate-per-device verification method. For more information, see Unique-certificate-per-device verification.

Initialize IoT Platform SDK for Java

Sample code:

#import <IotLinkKit/IotLinkKit.h>

    // Before you initialize the SDK, you must create a listener to monitor the status changes of a persistent connection.
    [[LinkKitEntry sharedKit] registerChannelListener:self];

    //// Specify the device certificate information.
    self.productKey = self.textFieldProductKey.text ;
    self.deviceName = self.textFieldDeviceName.text ;
    self.deviceSecret = self.textFieldDeviceSecret.text;

    LinkkitChannelConfig * channelConfig = [[LinkkitChannelConfig alloc] init];
    channelConfig.productKey = self.productKey;
    channelConfig.deviceName = self.deviceName;
    channelConfig.deviceSecret = self.deviceSecret;
    channelConfig.cleanSession = (self.cleanSession == 1);
    //channelConfig.server = @"your custom server url";
    //channelConfig.port = 1883;
    LinkkitSetupParams * setupParams = [[LinkkitSetupParams alloc] init];
    setupParams.appVersion = self.appVersion;
    setupParams.channelConfig = channelConfig;
    [[LinkKitEntry sharedKit] setup:setupParams resultBlock:^(BOOL succeeded, NSError * _Nullable error) {
        LinkkitLogDebug(@"setup error : %@", error);
        dispatch_async(dispatch_get_main_queue(), ^{
            [self ims_showHUDWithMessage:[NSString stringWithFormat:@ "Initialization :%@",
                                          succeeded ? @ "Success" : @ "Failure"]];
        });
    }];
            
Important
  • After you initialize the SDK, resultBlock is returned regardless of whether a persistent Message Queuing Telemetry Transport (MQTT) connection is established.

  • You can call the [[LinkKitEntry sharedKit] registerChannelListener:self] function to check whether a persistent connection is established based on the returned result of the [onConnectStateChange:state:] callback function.

Parameters

Parameter

Example

Description

productKey

a18wP******

The device certificate information that is saved on premises after you add the device.

You can also view the information on the Device Details page of the IoT Platform console. For more information, see Obtain device authentication information.

deviceName

LightSwitch

deviceSecret

uwMTmVAMnGGHaAkqmeDY6cHxxB******

server

iot-06z00ax1o******.mqtt.iothub.aliyuncs.com

The endpoint.

  • If you use an Enterprise Edition instance, or a public instance that is activated on July 30, 2021 or later, view the endpoint on the Instance Details page. Click View Development Configurations in the upper-right corner. On the Development Configurations panel, the endpoint is displayed.

  • If you use a public instance that is activated before July 30, 2021, the endpoint is ${YourProductKey} .iot-as-mqtt.${YourRegionId}.aliyuncs.com.

Deinitialize the SDK

Before you deinitialize the SDK, close the persistent MQTT connection between the device and IoT Platform. To deinitialize the SDK, call the following API operation.

    [[LinkKitEntry sharedKit] destroy:^(BOOL succeeded, NSError * _Nullable error) {
        LinkkitLogDebug(@"kit destroy error : %@", error);
        dispatch_async(dispatch_get_main_queue(), ^{
            [self ims_showHUDWithMessage:[NSString stringWithFormat:@ "Deinitialization: %@",
                                          succeeded ? @ "Success" : @ "Failure"]];
        });
    }];
            

Log switch

Turn on the internal log output switch of the SDK:


#import <IMSLog/IMSLog.h>

NSString * const IMS_DEMO_TAG = @"LinkkitDemo";

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Add the following code to enable the logging feature.
    [IMSLog setAllTagsLevel:IMSLogLevelAll];
    // Create a log panel and add it to the log framework.
    //[IMSLog addAssistant:[[IMSDashboardLogAssistant alloc] init]];
    // Enable logs to be displayed in the console.
    [IMSLog showInConsole:YES];
    // Specify a log tag.
    [IMSLog registerTag:IMS_DEMO_TAG];

    return YES;
}
            

Create a listener to monitor the status changes of the persistent MQTT connection.

Sample code:

- (void)onConnectStateChange:(nonnull NSString *)connectId state:(LinkkitChannelConnectState)state {
    NSString * connectTip = nil;
    if (state == LinkkitChannelStateConnected) {
        connectTip = @ "Connected";
    } else if (state == LinkkitChannelStateDisconnected) {
        connectTip = @ "Not connected";
    } else {
        connectTip = @ "Connecting";
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        self.labelConnectState.text = connectTip;
    });
}

- (void)onNotify:(nonnull NSString *)connectId topic:(nonnull NSString *)topic data:(id _Nullable)data {
    NSString * downData = [NSString stringWithFormat:@ "The pushed data is received, topic :%@ \r\n", topic];
    downData = [downData stringByAppendingString:[NSString stringWithFormat:@ "\r \ndata :%@", data]];

    LinkkitLogDebug(@"kit recv  topic : %@", topic);

    dispatch_async(dispatch_get_main_queue(), ^{
        self.textViewDownData.text = downData;
    });
}

- (BOOL)shouldHandle:(nonnull NSString *)connectId 
               topic:(nonnull NSString *)topic {
    return YES;
}