On your application targets “General” settings tab, in the “Frameworks, Libraries, and Embedded content” section, drag and drop CocoaMQTT.xcframework, CocoaAsyncSocket.xcframework and Starscream.xcframework from the Carthage/Build folder on disk. Then select “Embed & Sign”.
CocoaMQTT
MQTT v3.1.1 and v5.0 client library for iOS/macOS/tvOS written with Swift 5
Build
Build with Xcode 11.1 / Swift 5.1
IOS Target: 9.0 or above OSX Target: 10.12 or above TVOS Target: 10.0 or above
xcode 14.3 issue:
If you encounter the issue, Please update your project minimum depolyments to 11.0
Installation
CocoaPods
To integrate CocoaMQTT into your Xcode project using CocoaPods, you need to modify you
Podfile
like the followings:Then, run the following command:
At last, import “CocoaMQTT” to your project:
Carthage
Install using Carthage by adding the following lines to your Cartfile:
Then, run the following command:
At last:
On your application targets “General” settings tab, in the “Frameworks, Libraries, and Embedded content” section, drag and drop CocoaMQTT.xcframework, CocoaAsyncSocket.xcframework and Starscream.xcframework from the Carthage/Build folder on disk. Then select “Embed & Sign”.
Usage
Create a client to connect MQTT broker:
Now you can use closures instead of
CocoaMQTTDelegate
:SSL Secure
One-way certification
No certificate is required locally. If you want to trust all untrust CA certificates, you can do this:
Two-way certification
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:
MQTT over Websocket
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 executepod install
again:If you’re using CocoaMQTT in a project with only a
.podspec
and noPodfile
, e.g. in a module for React Native, add this line to your.podspec
:Then, Create a MQTT instance over Websocket:
If you want to add additional custom header to the connection, you can use the following:
Example App
You can follow the Example App to learn how to use it. But we need to make the Example App works fisrt:
Then, open the
Example.xcworkspace/
by Xcode and start it!Dependencies
These third-party functions are used:
GCDAsyncSocketLICENSE
MIT License (see
LICENSE
)Contributors
Author
Twitter
https://twitter.com/EMQTech