CourierMQTT Documentation
Welcome to the documentation for CourierMQTT, a lightweight and easy-to-use MQTT client for iOS apps. This documentation will guide you through the installation and usage of CourierMQTT in your projects.
Installation
To integrate CourierMQTT into your project, you can use CocoaPods. Simply add the following line to your Podfile:
“`plaintext
pod ‘CourierMQTT’
“`
Then, run the command `pod install`.
Alternatively, you can manually install CourierMQTT by following these steps:
1. Download the latest release from [GitHub](https://github.com/Courier-mqtt/Courier.mqtt-ios/releases).
2. Extract the zip file.
3. Drag and drop the `CourierMQTT.xcodeproj` file into your Xcode project.
4. In your build settings, add `libCourierMQTT.a` to the “Link Binary With Libraries” section.
5. Ensure that the framework is properly linked by verifying that `CourierMQTT.framework` is listed under “Embedded Binaries.”
Usage
To use CourierMQTT in your project, follow these steps:
1. Import `CourierMQTT` into your Swift file:
“`swift
import CourierMQTT
“`
2. Create an instance of `MQTTClient`:
“`swift
let client = MQTTClient(clientId: “your-client-id”, host: “mqtt-server-url”)
“`
3. Set the delegate to handle MQTT events:
“`swift
client.delegate = self
“`
4. Connect to the MQTT server:
“`swift
client.connect()
“`
5. Implement the `MQTTClientDelegate` protocol methods to handle subscription, disconnection, and message reception events:
“`swift
extension YourViewController: MQTTClientDelegate {
func mqttDidConnect(_ client: MQTTClient) {
// Handle successful connection
}
func mqttDidDisconnect(_ client: MQTTClient, withError error: Error?) {
// Handle disconnection and error, if any
}
func mqtt(_ client: MQTTClient, didReceiveMessage message: MQTTMessage) {
// Handle received message
}
}
“`
6. Subscribe to a topic:
“`swift
client.subscribe(to: “topic-name”)
“`
7. Publish a message:
“`swift
client.publish(message: “your-message”, to: “topic-name”)
“`
Security
CourierMQTT supports secure connections using SSL/TLS. To enable secure communication, follow these steps:
1. Set the `useSSL` property of the `MQTTClient` instance to true:
“`swift
client.useSSL = true
“`
2. Provide the necessary SSL certificates by setting the `caCertificate`, `clientCertificate`, and `privateKey` properties of the client. These properties should be set with the corresponding data or file paths:
“`swift
client.caCertificate = “path/to/ca_certificate.pem”
client.clientCertificate = “path/to/client_certificate.pem”
client.privateKey = “path/to/private_key.pem”
“`
Troubleshooting
If you encounter any issues while using CourierMQTT, here are some common troubleshooting steps:
1. Ensure that the MQTT server URL is correct and accessible.
2. Check that you have proper network connectivity.
3. Review your code for any potential errors or incorrect MQTT configurations.
4. Make sure you are subscribing to the correct topics and publishing messages to the appropriate topics.
That concludes the documentation for CourierMQTT. You should now have a basic understanding of how to integrate and use CourierMQTT in your iOS projects. Enjoy building powerful MQTT-based applications with ease!