I am building an iOS application and I wanted to use MQTT to be able to subscribe/publish messages to an MQTT Server. Upon researching MQTT Libraries, I found this library which seemed to be adequate for my purposes. Kindly note that I already made an MQTT Server online using CloudMQTT. After figuring out how to make CocoaPods work, and how to add dependencies on my Podfile, I finally got it up and running. In the viewDidLoad
function of my first view, I attempted to connect to my MQTT Server by adding this line to my .h
file:
@interface VBViewController : UIViewController<MQTTSessionDelegate>
and in my .m
file, in the viewDidLoad
function, I did this:
MQTTCFSocketTransport *transport = [[MQTTCFSocketTransport alloc] init];
transport.host = @"mPortNumber.cloudmqtt.com";
transport.port = portNumber;
session = [[MQTTSession alloc] init];
session.transport = transport;
session.delegate=self;
[session connectAndWaitTimeout:30];
[session subscribeToTopic:@"username/messagesFolder/#" atLevel:2 subscribeHandler:^(NSError *error, NSArray<NSNumber *> *gQoss){
if (error) {
NSLog(@"Subscription failed %@", error.localizedDescription);
} else {
NSLog(@"Subscription sucessfull! Granted Qos: %@", gQoss);
}
}];
However, upon running it, neither the Subscription fail/success messages showed up. I thought that I might have used the wrong port number, so I used the all three ports provided, the simple Port, an SSL Port, and a Websockets Port. After none of those worked, I tried to change the folder directory by playing with the slashes but that didn't work either.
I also tried sending and receiving messages but that didn't work out too.
I am using the emulator for the mean time since I haven't renewed my iOS Developer License yet. I don't want to blow all my 10 deployments just to figure this simple thing out. Might that be the cause of the failure to connect to my MQTT Server? I would doubt that because the emulator can connect to the internet and browse using Safari just fine - so it's pretty much connected to the internet.
Am I missing something with how I initialized/imported the MQTT methods?