1#import <Foundation/Foundation.h>
21@implementation MqttSessionExample
34- (void)initializeSdk {
43 [
self.sdk setUserHash:@"USER-HASH-HERE"];
48 [
self.sdk setServer:@"https://custom.navigine.com"];
53 self.locationManager = [
self.sdk getLocationManager];
58 self.navigationManager = [
self.sdk getNavigationManager:
self.locationManager];
63 self.mqttSession = [
self.sdk getMqttSession:
self.navigationManager];
67 NSLog(
@"MqttSession successfully initialized");
69 }
@catch (NSException *exception) {
70 NSLog(
@"Error initializing SDK: %@", exception.reason);
77- (void)demonstrateMqttSessionMethods {
79 NSLog(
@"MqttSession not initialized");
85 [
self.mqttSession addListener:
self];
91 [
self.mqttSession setSubTopic:@"location1"];
100 [
self.mqttSession connect:@"mqtt.example.com" port:1883 username:@"username" password:@"password"];
104 [NSThread sleepForTimeInterval:2.0];
109 NSString *customTopic =
@"custom/device/status";
110 NSString *customMessage = [NSString stringWithFormat:@"{\"status\": \"online\", \"timestamp\": %lld}", (long long)([[NSDate date] timeIntervalSince1970] * 1000)];
111 [
self.mqttSession publish:customTopic message:customMessage];
118- (void)demonstrateMqttSessionConfigurations {
120 NSLog(
@"MqttSession not initialized");
124 NSLog(
@"=== MQTT Session Configurations ===");
129 [
self.mqttSession setSubTopic:@"location1"];
133 [
self.mqttSession connect:@"mqtt.example.com" port:1883 username:@"user" password:@"pass"];
137 [NSThread sleepForTimeInterval:1.0];
142 [
self.mqttSession disconnect];
148 [
self.mqttSession setSubTopic:@"building-a"];
152 [
self.mqttSession connect:@"mqtt-secure.example.com" port:8883 username:@"user" password:@"pass"];
156 [NSThread sleepForTimeInterval:1.0];
161 [
self.mqttSession setSubTopic:@"floor_2"];
165 [
self.mqttSession connect:@"mqtt.example.com" port:1883 username:@"user" password:@"pass"];
172- (void)demonstrateListenerManagement {
174 NSLog(
@"MqttSession not initialized");
178 NSLog(
@"=== Listener Management ===");
186 [
self.mqttSession addListener:listener1];
191 [
self.mqttSession addListener:listener2];
196 [
self.mqttSession setSubTopic:@"location1"];
199 [
self.mqttSession connect:@"mqtt.example.com" port:1883 username:@"user" password:@"pass"];
203 [NSThread sleepForTimeInterval:2.0];
207 [
self.mqttSession publish:@"custom/events" message:@"{\"event\": \"user_action\"}"];
208 [
self.mqttSession publish:@"custom/logs" message:@"Application started"];
212 [NSThread sleepForTimeInterval:1.0];
216 [
self.mqttSession removeListener:listener1];
221 [
self.mqttSession removeListener:listener2];
232 [
self.mqttSession removeListener:
self];
237 [
self.mqttSession disconnect];
246 NSLog(
@"=== MqttSession Example ===");
248 [
self demonstrateMqttSessionMethods];
249 [
self demonstrateMqttSessionConfigurations];
250 [
self demonstrateListenerManagement];
253 [NSThread sleepForTimeInterval:3.0];
256 NSLog(
@"=== Example completed ===");
259#pragma mark - NCMqttSessionListener
263 NSLog(
@"MQTT session connected successfully");
268- (void)onError:(NSError *)error {
269 NSLog(
@"MQTT session error: %@", error.localizedDescription);
275 NSLog(
@"Message published successfully");
284@interface MqttSessionListenerImpl : NSObject <NCMqttSessionListener>
286@property (nonatomic, strong) NSString *name;
288- (instancetype)initWithName:(NSString *)name;
292@implementation MqttSessionListenerImpl
294- (instancetype)initWithName:(NSString *)name {
303 NSLog(
@"%@: Connected",
self.name);
306- (void)onError:(NSError *)error {
307 NSLog(
@"%@: Error - %@",
self.name, error.localizedDescription);
310- (void)onMessagePublished {
311 NSLog(
@"%@: Message published",
self.name);
319int main(
int argc,
const char * argv[]) {
322 [example runExample];