Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

MQTT 5.0 client library for iOS and macOS written in Swift

License

NotificationsYou must be signed in to change notification settings

emqx/CocoaMQTT

Repository files navigation

PodVersionPlatformsLicenseSwift version

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: 12.0 or aboveOSX Target: 10.13 or aboveTVOS Target: 10.0 or above

xcode 14.3 issue:

File notfound:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/arc/libarclite_iphonesimulator.a

If you encounter the issue, Please update your project minimum depolyments to 11.0

Installation

Swift Package Manager

To integrate CocoaMQTT into your Xcode project usingSwift Package Manager, follow these steps:

  1. Open your project in Xcode.
  2. Go toFile >Swift Packages >Add Package Dependency.
  3. Enter the repository URL:https://github.com/emqx/CocoaMQTT.git.
  4. Choose the latest version or specify a version range.
  5. Add the package to your target.

At last, import "CocoaMQTT" to your project:

import CocoaMQTT

CocoaPods

To integrate CocoaMQTT into your Xcode project usingCocoaPods, you need to modify youPodfile like the followings:

use_frameworks!target'Example'dopod'CocoaMQTT'end

Then, run the following command:

$ pod install

At last, import "CocoaMQTT" to your project:

import CocoaMQTT

Carthage

Install usingCarthage by adding the following lines to your Cartfile:

github "emqx/CocoaMQTT" "master"

Then, run the following command:

$ carthage update --platform iOS,macOS,tvOS --use-xcframeworks

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 connectMQTT broker:

///MQTT 5.0letclientID="CocoaMQTT-"+ String(ProcessInfo().processIdentifier)letmqtt5=CocoaMQTT5(clientID: clientID, host:"broker.emqx.io", port:1883)letconnectProperties=MqttConnectProperties()connectProperties.topicAliasMaximum=0connectProperties.sessionExpiryInterval=0connectProperties.receiveMaximum=100connectProperties.maximumPacketSize=500mqtt5.connectProperties= connectPropertiesmqtt5.username="test"mqtt5.password="public"mqtt5.willMessage=CocoaMQTTMessage(topic:"/will", string:"dieout")mqtt5.keepAlive=60mqtt5.delegate=selfmqtt5.connect()///MQTT 3.1.1letclientID="CocoaMQTT-"+ String(ProcessInfo().processIdentifier)letmqtt=CocoaMQTT(clientID: clientID, host:"broker.emqx.io", port:1883)mqtt.username="test"mqtt.password="public"mqtt.willMessage=CocoaMQTTMessage(topic:"/will", string:"dieout")mqtt.keepAlive=60mqtt.delegate=selfmqtt.connect()

Now you can use closures instead ofCocoaMQTTDelegate:

mqtt.didReceiveMessage={ mqtt, message, idinprint("Message received in topic\(message.topic) with payload\(message.string!)")}

SSL Secure

One-way certification

No certificate is required locally.If you want to trust all untrust CA certificates, you can do this:

mqtt.allowUntrustCACertificate=true

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:

openssl pkcs12 -export -clcerts -in client-cert.pem -inkey client-key.pem -out client.p12

Note: Please use openssl version 1.1 (e.g.brew install openssl@1.1), otherwise you may not be able to import the generated .p12 file to the system correctly.

MQTT over Websocket

In the 1.3.0, The CocoaMQTT has supported to connect to MQTT Broker by Websocket.

If you integrated bySwift Package Manager, follow these steps:

  1. Open your project in Xcode.
  2. Go toFile >Swift Packages >Add Package Dependency.
  3. Enter the repository URL:https://github.com/emqx/CocoaMQTT.git.
  4. Choose the latest version or specify a version range.
  5. Add the package to your target.

At last, import "CocoaMQTT" and "Starscream" to your project:

import CocoaMQTTimport CocoaMQTTWebSocketimport Starscream

If you integrated byCocoaPods, you need to modify youPodfile like the followings and executepod install again:

use_frameworks!target'Example'dopod'CocoaMQTT/WebSockets'end

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:

Pod::Spec.newdo |s|  ...s.dependency"Starscream"end

Then, Create a MQTT instance over Websocket:

///MQTT 5.0letwebsocket=CocoaMQTTWebSocket(uri:"/mqtt")letmqtt5=CocoaMQTT5(clientID: clientID, host: host, port:8083, socket: websocket)letconnectProperties=MqttConnectProperties()connectProperties.topicAliasMaximum=0// ...mqtt5.connectProperties= connectProperties// ..._= mqtt5.connect()///MQTT 3.1.1letwebsocket=CocoaMQTTWebSocket(uri:"/mqtt")letmqtt=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:

letwebsocket=CocoaMQTTWebSocket(uri:"/mqtt")websocket.headers=["x-api-key":"value"]        websocket.enableSSL=trueletmqtt=CocoaMQTT(clientID: clientID, host: host, port:8083, socket: websocket)// ..._= mqtt.connect()

If you want to connect using WebSocket Secure (wss), you can use the following example:

import CocoaMQTTimport CocoaMQTTWebSocketimport StarscreamclassWebSocketManager{privatevarmqttClient:CocoaMQTT?varmessage:String=""vartoken:String=""func setupMQTTClient(with token:String){letsocket=CocoaMQTTWebSocket(uri:"/mqtt")        socket.enableSSL=true        mqttClient=CocoaMQTT(clientID: token, host:"host", port:443, socket: socket)        mqttClient?.delegate=self}func connect(){guardlet mqttClient= mqttClientelse{return}        mqttClient.connect()}}extensionWebSocketManager:CocoaMQTTDelegate{func mqtt(_ mqtt:CocoaMQTT, didReceive trust:SecTrust, completionHandler:@escaping(Bool)->Void){        // Implement your custom SSL validation logic here.        // For example, you might want to always trust the certificate for testing purposes:completionHandler(true)}func mqtt(_ mqtt:CocoaMQTT, didReceive challenge:URLAuthenticationChallenge, completionHandler:@escaping(URLSession.AuthChallengeDisposition,URLCredential?)->Void){if challenge.protectionSpace.authenticationMethod== NSURLAuthenticationMethodServerTrust{iflet serverTrust= challenge.protectionSpace.serverTrust{completionHandler(.useCredential,URLCredential(trust: serverTrust))return}}completionHandler(.performDefaultHandling,nil)}func mqttUrlSession(_ mqtt:CocoaMQTT, didReceiveTrust trust:SecTrust, didReceiveChallenge challenge:URLAuthenticationChallenge, completionHandler:@escaping(URLSession.AuthChallengeDisposition,URLCredential?)->Void){print("\(#function),\n result:-\(challenge.debugDescription)")}func mqtt(_ mqtt:CocoaMQTT, didPublishAck id:UInt16){print("Published message with ID:\(id)")}func mqtt(_ mqtt:CocoaMQTT, didUnsubscribeTopics topics:[String]){print("Unsubscribed from topics:\(topics)")}func mqttDidPing(_ mqtt:CocoaMQTT){print("MQTT did ping")}func mqttDidReceivePong(_ mqtt:CocoaMQTT){print("MQTT did receive pong")}func mqttDidDisconnect(_ mqtt:CocoaMQTT, withError err:(anyError)?){print("Disconnected from MQTT broker with error:\(String(describing: err))")}func mqtt(_ mqtt:CocoaMQTT, didConnectAck ack:CocoaMQTTConnAck){print("Connected to MQTT broker with acknowledgment:\(ack)")}func mqtt(_ mqtt:CocoaMQTT, didReceiveMessage message:CocoaMQTTMessage, id:UInt16){iflet messageString= message.string{DispatchQueue.main.async{self.message= messageString}print("Received message:\(messageString) on topic:\(message.topic)")}}func mqtt(_ mqtt:CocoaMQTT, didPublishMessage message:CocoaMQTTMessage, id:UInt16){print("Published message:\(message.string??"") with ID:\(id)")}func mqtt(_ mqtt:CocoaMQTT, didSubscribeTopics success:NSDictionary, failed:[String]){print("Subscribed to topics:\(success), failed to subscribe to:\(failed)")}func mqtt(_ mqtt:CocoaMQTT, didDisconnectWithError err:Error?){print("Disconnected from MQTT broker with error:\(String(describing: err))")}}

Example App

You can follow the Example App to learn how to use it. But we need to make the Example App works first:

$cd ExamplesThen, open the`Example.xcodeproj` by Xcode and start it!## DependenciesThese third-party functions are used:~~[GCDAsyncSocket](https://github.com/robbiehanson/CocoaAsyncSocket)~~* [MqttCocoaAsyncSocket](https://github.com/leeway1208/MqttCocoaAsyncSocket)* [Starscream](https://github.com/daltoniam/Starscream)## LICENSEMIT License (see`LICENSE`)## Contributors* [@andypiper](https://github.com/andypiper)* [@turtleDeng](https://github.com/turtleDeng)* [@jan-bednar](https://github.com/jan-bednar)* [@jmiltner](https://github.com/jmiltner)* [@manucheri](https://github.com/manucheri)* [@Cyrus Ingraham](https://github.com/cyrusingraham)## Author- Feng Lee<feng@emqx.io>- CrazyWisdom<zh.whong@gmail.com>- Alex Yu<alexyu.dc@gmail.com>- Leeway<leeway1208@gmail.com>## Twitterhttps://twitter.com/EMQTech

About

MQTT 5.0 client library for iOS and macOS written in Swift

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp