@@ -36,7 +36,7 @@ - (void)viewDidLoad
3636 // block below (retain/release cycle, blah blah blah)
3737 UISwitch *subSwitch = self.subscribedSwitch ;
3838
39- // define the handler that will be called when MQTT messages are receive by the client
39+ // define the handler that will be called when MQTT messages are received by the client
4040 [self .client setMessageHandler: ^(MQTTMessage *message) {
4141 // extract the switch status from the message payload
4242 BOOL on = [message.payloadString boolValue ];
@@ -49,14 +49,16 @@ - (void)viewDidLoad
4949 }];
5050
5151 // connect the MQTT client
52- [self .client connectToHost: kMQTTServerHost completionHandler: ^(NSUInteger code) {
53- // The client is connected when this completion handler is called
54- NSLog (@" client is connected with id %@ " , clientID);
55- // Subscribe to the topic
56- [self .client subscribe: kTopic withCompletionHandler: ^(NSArray *grantedQos) {
57- // The client is effectively subscribed to the topic when this completion handler is called
58- NSLog (@" subscribed to topic %@ " , kTopic );
59- }];
52+ [self .client connectToHost: kMQTTServerHost completionHandler: ^(MQTTConnectionReturnCode code) {
53+ if (code == ConnectionAccepted) {
54+ // The client is connected when this completion handler is called
55+ NSLog (@" client is connected with id %@ " , clientID);
56+ // Subscribe to the topic
57+ [self .client subscribe: kTopic withCompletionHandler: ^(NSArray *grantedQos) {
58+ // The client is effectively subscribed to the topic when this completion handler is called
59+ NSLog (@" subscribed to topic %@ " , kTopic );
60+ }];
61+ }
6062 }];
6163}
6264
0 commit comments