MQTT v3.1.1 and v5.0 client library for iOS/macOS/tvOS written with Swift 5
Build with Xcode 11.1 / Swift 5.1
IOS Target: 10.0 or above OSX Target: 10.12 or above TVOS Target: 10.0 or above
Install using CocoaPods by adding this line to your Podfile:
pod 'CocoaMQTT, '~> 2.0.1''
Then, run the following command:
$ pod install
At last, import "CocoaMQTT" to your project:
import CocoaMQTT
Install using Carthage by adding the following lines to your Cartfile:
github "emqx/CocoaMQTT" "master"
Then, run the following command:
$ carthage update --platform iOS,macOS,tvOS
Last if you're building for OS X:
If you're building for iOS, tvOS:
On your application targets “General” settings tab, in the "Frameworks and Libraries" section, drag and drop each framework you want to use from the Carthage/Build folder on disk.
On your application targets "Build Phases" settings tab, click the "+" icon and choose "New Run Script Phase". Create a Run Script with the following contents:
/usr/local/bin/carthage copy-frameworks
and add the paths to the frameworks you want to use under "Input Files", e.g.:
$(SRCROOT)/Carthage/Build/iOS/CocoaMQTT.framework
Create a client to connect MQTT broker:
///MQTT 5.0
let clientID = "CocoaMQTT-" + String(ProcessInfo().processIdentifier)
let mqtt5 = CocoaMQTT5(clientID: clientID, host: "localhost", port: 1883)
let connectProperties = MqttConnectProperties()
connectProperties.topicAliasMaximum = 0
connectProperties.sessionExpiryInterval = 0
connectProperties.receiveMaximum = 100
connectProperties.maximumPacketSize = 500
mqtt5.connectProperties = connectProperties
mqtt5.username = "test"
mqtt5.password = "public"
mqtt5.willMessage = CocoaMQTTWill(topic: "/will", message: "dieout")
mqtt5.keepAlive = 60
mqtt5.delegate = self
mqtt5.connect()
///MQTT 3.1.1
let clientID = "CocoaMQTT-" + String(ProcessInfo().processIdentifier)
let mqtt = CocoaMQTT(clientID: clientID, host: "localhost", port: 1883)
mqtt.username = "test"
mqtt.password = "public"
mqtt.willMessage = CocoaMQTTWill(topic: "/will", message: "dieout")
mqtt.keepAlive = 60
mqtt.delegate = self
mqtt.connect()
Now you can use closures instead of CocoaMQTTDelegate
:
mqtt.didReceiveMessage = { mqtt, message, id in
print("Message received in topic \(message.topic) with payload \(message.string!)")
}
No certificate is required locally. If you want to trust all untrust CA certificates, you can do this:
mqtt.allowUntrustCACertificate = true
Need a .p12 file which is generated by a public key file and a private key file. You can generate the p12 file in the terminal:
openssl pkcs12 -export -clcerts -in client-cert.pem -inkey client-key.pem -out client.p12
In the 1.3.0, The CocoaMQTT has supported to connect to MQTT Broker by Websocket.
If you integrated by CocoaPods, you need to modify you Podfile
like the followings and execute pod install
again:
use_frameworks!
target 'Example' do
pod 'CocoaMQTT/WebSockets', '1.3.0-rc.1'
end
If you're using CocoaMQTT in a project with only a .podspec
and no Podfile
, e.g. in a module for React Native, add this line to your .podspec
:
Pod::Spec.new do |s|
...
s.dependency "Starscream", "~> 3.1.1"
end
Then, Create a MQTT instance over Websocket:
///MQTT 5.0
let websocket = CocoaMQTTWebSocket(uri: "/mqtt")
let mqtt5 = CocoaMQTT5(clientID: clientID, host: host, port: 8083, socket: websocket)
let connectProperties = MqttConnectProperties()
connectProperties.topicAliasMaximum = 0
// ...
mqtt5.connectProperties = connectProperties
// ...
_ = mqtt5.connect()
///MQTT 3.1.1
let websocket = CocoaMQTTWebSocket(uri: "/mqtt")
let mqtt = CocoaMQTT(clientID: clientID, host: host, port: 8083, socket: websocket)
// ...
_ = mqtt.connect()
If you want to add additional custom header to the connection, you can use the following:
let websocket = CocoaMQTTWebSocket(uri: "/mqtt")
websocket.headers = [
"x-api-key": "value"
]
websocket.enableSSL = true
let mqtt = CocoaMQTT(clientID: clientID, host: host, port: 8083, socket: websocket)
// ...
_ = mqtt.connect()
You can follow the Example App to learn how to use it. But we need to make the Example App works fisrt:
$ cd Examples
$ pod install
Then, open the Example.xcworkspace/
by Xcode and start it!
These third-party functions are used:
MIT License (see LICENSE
)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。