Communication by using MQTT topics
Link SDK for iOS provides basic API operations that you can call to establish persistent connections between devices and IoT Platform. You can call the API operations to use custom topics. You can call the API operations of Link SDK for iOS to perform the following operations: publish messages, subscribe to topics, unsubscribe from topics, and subscribe to downstream messages. The API operations also allow you to send data to or receive data from the cloud.
Submit data to IoT Platform
Before you call an API operation that is described in this topic, make sure that a Message Queuing Telemetry Transport (MQTT) persistent connection is established.
Link SDK for iOS encapsulates the API operations that you can call to send upstream requests. Sample code:
IoT Platform supports only quality of service (QoS) 0 and QoS 1 messages. IoT Platform applies throttling to upstream and downstream data. For more information, see Limits.
NSString * topic = self.textFieldPubTopic.text;
NSString * content = self.textViewPubContent.text;
NSInteger iQos = [self.textFieldQos.text integerValue];//qos = 0 or 1
[[LinkKitEntry sharedKit] publish:topic
data:[content dataUsingEncoding:NSUTF8StringEncoding]
qos:(int)iQos
resultBlock:^(BOOL succeeded, NSError * _Nullable error) {
LinkkitLogDebug(@"kit publish error : %@", error);
dispatch_async(dispatch_get_main_queue(), ^{
[self ims_showHUDWithMessage:[NSString stringWithFormat:@ "Upstream data: %@",
succeeded ? @ "Success" : @ "Failure"]];
});
}];
Subscribe to or unsubscribe from a topic
If you want to receive data from a topic, you must subscribe to the topic. If you no longer need to receive data from a topic, you can unsubscribe from the topic.
The following sample code shows how to subscribe to a topic:
- (IBAction)onClickSubscribe:(id)sender {
NSString * topic = self.textFieldSubTopic.text;
[[LinkKitEntry sharedKit] subscribe:topic
resultBlock:^(BOOL succeeded, NSError * _Nullable error) {
LinkkitLogDebug(@"kit subscribe error : %@", error);
dispatch_async(dispatch_get_main_queue(), ^{
[self ims_showHUDWithMessage:[NSString stringWithFormat:@ "Subscribe to a topic: %@",
succeeded ? @ "Success" : @ "Failure"]];
});
}];
}
The following sample code shows how to unsubscribe from a topic:
- (IBAction)onClickUnsubscribe:(id)sender {
NSString * topic = self.textFieldSubTopic.text;
[[LinkKitEntry sharedKit] unsubscribe:topic
resultBlock:^(BOOL succeeded, NSError * _Nullable error) {
LinkkitLogDebug(@"kit unsubscribe error : %@", error);
dispatch_async(dispatch_get_main_queue(), ^{
[self ims_showHUDWithMessage:[NSString stringWithFormat:@ "Unsubscribe from a topic: %@",
succeeded ? @ "Success" : @ "Failure"]];
});
}];
}
Listen to downstream data from IoT Platform
After you subscribe to a topic, you must register a listener to listen to downstream messages of the topic. Then, Link SDK for iOS returns the messages of the topic. For more information about the sample code for registering a listener to listen to downstream messages, see Verify and connect a device.