Authentication and connection
This topic describes how to initialize the iOS Link SDK and establish a connection between a device and IoT Platform.
Background information
The iOS Link SDK supports only device authentication that uses one secret per device. For more information, see One secret per device.
Initialize the SDK
Sample code:
#import <IotLinkKit/IotLinkKit.h>
// Before initialization, register a listener to monitor the connection status of the persistent connection channel.
[[LinkKitEntry sharedKit] registerChannelListener:self];
//// Enter 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:@"Linkkit initialization: %@",
succeeded ? @"succeeded" : @"failed"]];
});
}];
A successful callback of
resultBlockduring initialization does not indicate that the MQTT persistent connection channel is established.Call the
[[LinkKitEntry sharedKit] registerChannelListener:self]method. Then, use the[onConnectStateChange:state:]callback method of the listener to determine whether the connection is successful.
Parameters
Parameter | Example | Description |
productKey | a18wP****** | The device certificate information. This is the device certificate that you save locally after you add the device. You can also view the device credential information on the Device Details page in the IoT Platform console. For more information, see Obtain device credentials. |
deviceName | LightSwitch | |
deviceSecret | uwMTmVAMnGGHaAkqmeDY6cHxxB****** | |
server | a18wP******.iot-as-mqtt.cn-shanghai.aliyuncs.com | The domain name for device connection.
For more information about new and legacy public instances, Enterprise instances, and domain names for device connection, see View instance endpoints. |
Deinitialize the SDK
Deinitialization disconnects the MQTT persistent connection between the device and IoT Platform. To deinitialize the SDK, call the following method.
[[LinkKitEntry sharedKit] destroy:^(BOOL succeeded, NSError * _Nullable error) {
LinkkitLogDebug(@"kit destroy error : %@", error);
dispatch_async(dispatch_get_main_queue(), ^{
[self ims_showHUDWithMessage:[NSString stringWithFormat:@"Linkkit deinitialization: %@",
succeeded ? @"succeeded" : @"failed"]];
});
}];
Log switch
Enable the log switch to output internal SDK logs:
#import <IMSLog/IMSLog.h>
NSString * const IMS_DEMO_TAG = @"LinkkitDemo";
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Add the following code to enable console logs for the SDK.
[IMSLog setAllTagsLevel:IMSLogLevelAll];
// Create a log panel assistant and inject it into the log frame.
//[IMSLog addAssistant:[[IMSDashboardLogAssistant alloc] init]];
// Set logs to be displayed in the console.
[IMSLog showInConsole:YES];
// Set the log tag for the demo.
[IMSLog registerTag:IMS_DEMO_TAG];
return YES;
}
Listener for MQTT persistent connection channel status changes
Sample code:
- (void)onConnectStateChange:(nonnull NSString *)connectId state:(LinkkitChannelConnectState)state {
NSString * connectTip = nil;
if (state == LinkkitChannelStateConnected) {
connectTip = @"Connected";
} else if (state == LinkkitChannelStateDisconnected) {
connectTip = @"Disconnected";
} 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:@"Received a push message. 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;
}