MQTT v3.1.1和V5.0 iOS/MACOS/TVOS的客户端库,用Swift 5编写
使用Xcode 11.1 / Swift 5.1构建
iOS目标:12.0或以上OSX目标:10.13或以上TVOS目标:10.0或更高
File not found : /Applications/ Xcode . app / Contents / Developer / Toolchains / XcodeDefault . xctoolchain / usr / lib / arc / libarclite_iphonesimulator . a如果您遇到问题,请将您的项目最小间距更新为11.0
要使用Cocoapods将CocoAmqtt集成到您的Xcode项目中,您需要像以下内容一样修改Podfile :
use_frameworks!
target 'Example' do
pod 'CocoaMQTT'
end然后,运行以下命令:
$ pod install最后,向您的项目导入“ CocoAmqtt”:
import CocoaMQTT通过将以下几行添加到您的Cartfile:使用迦太基安装:
github "emqx/CocoaMQTT" "master"
然后,运行以下命令:
$ carthage update --platform iOS,macOS,tvOS --use-xcframeworks终于:
在您的应用程序目标上,“常规”设置选项卡,“框架,库和嵌入式内容”部分,拖放CocoAmqtt.xcframework,cocoaasyncsocket.xcframework.xcframework和starstCrame.xcframe.xcframework从carthage/build build build insk On Disk上的构建文件机。然后选择“嵌入和签名”。
创建一个客户以连接MQTT代理:
///MQTT 5.0
let clientID = " CocoaMQTT- " + String ( ProcessInfo ( ) . processIdentifier )
let mqtt5 = CocoaMQTT5 ( clientID : clientID , host : " broker.emqx.io " , 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 = CocoaMQTTMessage ( topic : " /will " , string : " 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 : " broker.emqx.io " , port : 1883 )
mqtt . username = " test "
mqtt . password = " public "
mqtt . willMessage = CocoaMQTTMessage ( topic : " /will " , string : " dieout " )
mqtt . keepAlive = 60
mqtt . delegate = self
mqtt . connect ( )现在,您可以使用封闭而不是CocoaMQTTDelegate :
mqtt . didReceiveMessage = { mqtt , message , id in
print ( " Message received in topic ( message . topic ) with payload ( message . string! ) " )
} 本地不需要证书。如果您想信任所有不信任的CA证书,则可以执行此操作:
mqtt . allowUntrustCACertificate = true需要一个.p12文件,该文件由公共密钥文件和私钥文件生成。您可以在终端中生成p12文件:
openssl pkcs12 -export -clcerts -in client-cert.pem -inkey client-key.pem -out client.p12
注意:请使用OPENSL版本1.1(例如brew install [email protected] ),否则您可能无法将生成的.p12文件正确导入到系统中。
在1.3.0中,CocoAMQTT支持Websocket连接到MQTT代理。
如果您通过Cocoapods集成,则需要像以下内容一样修改Podfile ,然后再次执行pod install :
use_frameworks!
target 'Example' do
pod 'CocoaMQTT/WebSockets'
end如果您在一个项目中仅使用.podspec且没有Podfile CocoAmqtt,例如,在模块中,用于React Native,请将此行添加到您的.podspec :
Pod :: Spec . new do | s |
...
s . dependency "Starscream"
end然后,通过WebSocket创建一个MQTT实例:
///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 ( )如果要在连接中添加其他自定义标头,则可以使用以下内容:
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 ( ) 您可以关注示例应用程序以了解如何使用它。但是我们需要使示例应用程序起作用fisrt:
$ cd Examples
$ pod install然后,打开Example.xcworkspace/ xcode并开始它!
使用这些第三方功能:
gcdasyncsocket
麻省理工学院许可证(请参阅LICENSE )
https://twitter.com/emqtech