https://cocoapods.org/pods/SwiftMQTT
http://cocoadocs.org/docsets/SwiftMQTT/1.0.2/index.html
Problems not finding module
In case it’s Friday afternoon or anytime after 1am:
Opening xcodeproj
instead of xcworkspace
will cause an error like this…
Also adding Home as target in products->Home.app
Create Podfile
Download https://cocoapods.org/app
% cd % cd Documents/XCode/Home % vi Podfile
workspace 'Home' target 'Home' do use_frameworks! project 'Home.xcodeproj' use_frameworks! pod 'SwiftMQTT' end
% pod install
import UIKit import SwiftMQTT class ViewController: UIViewController, MQTTSessionDelegate { func mqttDidAcknowledgePing(from session: MQTTSession) { } let mqttSession = MQTTSession( host: "192.168.0.114", port: 1883, clientID: "swift", // must be unique to the client cleanSession: true, keepAlive: 15, useSSL: false ) override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. _ = mqttSession.connect { error in if error == .none { print("Connected!") } else { print(error.description) } } //mqttSession.delegate = self as? MQTTSessionDelegate mqttSession.delegate = self sleep(1) let topic = "mytopic" mqttSession.subscribe(to: topic, delivering: .atLeastOnce) { error in if error == .none { print("Subscribed to \(topic)!") } else { print("Subscribe error: \(error.description)") } } let json = ["key" : "value"] let data = try! JSONSerialization.data(withJSONObject: json, options: .prettyPrinted) //let topic = "mytopic" mqttSession.publish(data, in: topic, delivering: .atLeastOnce, retain: false) { error in if error == .none { print("Published data in \(topic)!") } else { print(error.description) } } } /* func mqttSession(session: MQTTSession, didReceiveMessage message: NSData, onTopic topic: String) { let stringData = NSString(data: message as Data, encoding: String.Encoding.utf8.rawValue)! as String print("data received on topic \(topic) message \(stringData)") }*/ func mqttDidReceive(message: MQTTMessage, from session: MQTTSession) { print(message.topic) print(message.stringRepresentation) } func mqttDidDisconnect(session: MQTTSession, error: MQTTSessionError) { if error == .none { print("Successfully disconnected from MQTT broker") } else { print(error.description) } } }