Connecting to the MQTT server and subscribing to topics in Swift
Now, we will add code to the ViewController
class to make it conform to the CocoaMQTTDelegate
protocol. In addition, we will add code to configure the MQTT client and establish a connection with the MQTT server. We will split the code for the ViewController
class into many code snippets to make it easier to understand each code section. The following lines specify all the necessary imports, start the ViewController
class declaration, and create many static type properties, the mqttClient
property, and the configureMQTTAndConnect
method. The following lines show the code for the ViewController
class defined in the ViewController.swift
file. The code file for the sample is included in the mqtt_essentials_gaston_hillar_06/ControlActuators
folder:
import UIKit import CocoaMQTT class ViewController: UIViewController, CocoaMQTTDelegate { @IBOutlet weak var statusLabel: UILabel! @IBOutlet weak var motorSwitch: UISwitch! ...