# iOS Swift Cloud HiveMQ example

**URL:** <https://community.hivemq.com/t/ios-swift-cloud-hivemq-example/933>\
**Category:** HiveMQ Cloud\
**Created:** [January 7, 2022, 5:30pm UTC](https://community.hivemq.com/t/ios-swift-cloud-hivemq-example/933 "2022-01-07T17:30:23Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgs227](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/mgs227/32/239_2.png) [@mgs227](https://community.hivemq.com/u/mgs227)\
**Post date:** [January 7, 2022, 5:30pm UTC](https://community.hivemq.com/t/ios-swift-cloud-hivemq-example/933/1 "2022-01-07T17:30:24Z")

</div>

Does anyone have an example of a iOS Swift application connecting to HiveMQ Cloud?

I am unable to connect -  
When using MQTT v3.x - I got an error code of 5. (Not Authorized)  
When using MQTT v5.x - I got an error code of 132 (Not Authorized)

I am trying to figure out the flags needed/not needed to connect via port 8883 - without the server certificate - similar to the mosquito-sub and mosquito-pub client apps., which both run on a mac without the need to specify the server.pem file

This is with CocoaMQTT:

Here is the code snippet that I was trying:

```auto
        var mqtt: CocoaMQTT5!
       
        let clientID = "CocoaMQTT-" + String(ProcessInfo().processIdentifier)
        let authProperties = MqttAuthProperties()
        let sslSettings: [String: NSObject] = [:]
        let server = "{YOUR INSTANCE}.s1.eu.hivemq.cloud"

        mqtt = CocoaMQTT5(clientID: clientID, host: server, port: 8883)
        mqtt.username = "{YOUR USERNAME}"
        mqtt.password = "{YOUR PASSWORD}"
        mqtt.willMessage = CocoaMQTT5Message(topic: "{YOUR TOPIC}", string: "{YOUR MESSAGE}")
        
        mqtt.keepAlive = 60
        mqtt.autoReconnect = true
        mqtt.enableSSL = true
        mqtt.allowUntrustCACertificate = true
        
                
        mqtt.sslSettings = sslSettings
        mqtt.auth(reasonCode: CocoaMQTTAUTHReasonCode.continueAuthentication, authProperties: authProperties)
        mqtt.connect()

```

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [January 10, 2022, 10:52am UTC](https://community.hivemq.com/t/ios-swift-cloud-hivemq-example/933/2 "2022-01-10T10:52:48Z")

</div>

Hi Michael,

Thanks for your interest in MQTT and HiveMQ! Unfortunately I do not have a Swift example at hand.  
To troubleshoot the issue, first please confirm, if you can connect your cloud broker from a known MQTT Client. We suggest using the [MQTT CLI](https://console.hivemq.cloud/clients/mqtt-cli):

```auto
#start MQTT CLI in Shell Mode:
mqtt sh
#connect to the cloud broker:
con -h ${YOUR INSTANCE}.s1.eu.hivemq.cloud -p 8883 -s -u ${YOUR USER} -pw ${YOUR PASSWORD}

```

(To get an overview of communication between the client and the broker, you can use Wireshark with a capture filter `tcp.port in {8883}`)

Please confirm if you connect your cloud broker with MQTT CLI.

Thanks ,  
Dasha from HiveMQ team

---

<div class="post-metadata">

**Author:** ![mgs227](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/mgs227/32/239_2.png) [@mgs227](https://community.hivemq.com/u/mgs227)\
**Post date:** [January 10, 2022, 2:13pm UTC](https://community.hivemq.com/t/ios-swift-cloud-hivemq-example/933/3 "2022-01-10T14:13:18Z")

</div>

I am able to connect to HiveMq Cloud - using port 8883, no certificate, using mosquitto\_sub and mosquitto\_pub from a mac computer.

I cannot connect from a swift application.

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [January 10, 2022, 2:53pm UTC](https://community.hivemq.com/t/ios-swift-cloud-hivemq-example/933/4 "2022-01-10T14:53:42Z")

</div>

Hi Michael,

Thank you for confirming this. One quick question: did you try your code with `mqtt.allowUntrustCACertificate = false`? Is the error any different?

Thanks,  
Dasha from HiveMQ team

---

<div class="post-metadata">

**Author:** ![mgs227](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/mgs227/32/239_2.png) [@mgs227](https://community.hivemq.com/u/mgs227)\
**Post date:** [January 10, 2022, 3:35pm UTC](https://community.hivemq.com/t/ios-swift-cloud-hivemq-example/933/5 "2022-01-10T15:35:38Z")

</div>

Here are the parameters I used, and the results:

```auto

mqtt.autoReconnect = true
mqtt.enableSSL = true
mqtt.allowUntrustCACertificate = false

```

```auto

State Change: connecting
135
State Change: disconnected

```

---

<div class="post-metadata">

**Author:** ![mgs227](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/mgs227/32/239_2.png) [@mgs227](https://community.hivemq.com/u/mgs227)\
**Post date:** [January 10, 2022, 4:23pm UTC](https://community.hivemq.com/t/ios-swift-cloud-hivemq-example/933/6 "2022-01-10T16:23:29Z")

</div>

Then I tried:

```auto
        mqtt.autoReconnect = true
        mqtt.enableSSL = false
        mqtt.allowUntrustCACertificate = false

```

```auto

State Change: connecting
State Change: disconnected

```

The disconnect happens right away

Then I tried:

```auto
        mqtt.autoReconnect = true
        mqtt.enableSSL = true
        mqtt.allowUntrustCACertificate = true

```

```auto

State Change: connecting
State Change: disconnected

```

The disconnect takes about 1 min to show up

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [January 11, 2022, 10:15am UTC](https://community.hivemq.com/t/ios-swift-cloud-hivemq-example/933/7 "2022-01-11T10:15:29Z")

</div>

Hi Michael,

Thank you for checking. I have also tested the CocoaMQTT library’s simpleSSL connection to the HiveMQ broker and it does not work, while the HiveMQ MQTT Client library works well. There might be something wrong (or outdated) with CocoaMQTT implementation of the simple SSL.  
Unfortunately we do not have a Swift library of our own. I am sorry that I could not help you more.

Kind regards,  
Dasha from HiveMQ team

---

<div class="post-metadata">

**Author:** ![gallaugher](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/gallaugher/32/548_2.png) [@gallaugher](https://community.hivemq.com/u/gallaugher)\
**Post date:** [January 10, 2023, 12:56am UTC](https://community.hivemq.com/t/ios-swift-cloud-hivemq-example/933/8 "2023-01-10T00:56:38Z")

</div>

Were you ever able to get a Swift HiveMQ example working with Swift? I’ve previously written an app to work with mosquitto on a Raspberry Pi, but I’d like to use HiveMQ as the broker. I can get HiveMQ to send messages to a Raspberry Pi Pico W microcontroller using the web client, but before I begin the work to try to create an MQTT5 client app in Swift, I’d like to confirm someone actually got this done before using CocoaMQTT (or other package). Thanks for your advice!

---

<div class="post-metadata">

**Author:** ![qwert477](https://avatars.discourse-cdn.com/v4/letter/q/c77e96/32.png) [@qwert477](https://community.hivemq.com/u/qwert477)\
**Post date:** [August 17, 2024, 3:49am UTC](https://community.hivemq.com/t/ios-swift-cloud-hivemq-example/933/9 "2024-08-17T03:49:14Z")

</div>

Hi @gallaugher @mgs227 @Daria_H  
Were you able to get a Swift HiveMQ example working? Please confirm before I invest time implementing CocoaMQTT. If not, could you suggest alternative libraries that have worked seamlessly?

---

<div class="post-metadata">

**Author:** ![mgs227](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/mgs227/32/239_2.png) [@mgs227](https://community.hivemq.com/u/mgs227)\
**Post date:** [August 17, 2024, 8:31pm UTC](https://community.hivemq.com/t/ios-swift-cloud-hivemq-example/933/10 "2024-08-17T20:31:46Z")

</div>

I was able to get a swift app - running CocoaMQTT - to work, but connecting to a generic MQTT 5.0 server. I have not tried my app connecting to HiveMQ. Not sure if this helps you or not.

---

<div class="post-metadata">

**Author:** ![AaronTLFranz](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/aarontlfranz/32/638_2.png) [@AaronTLFranz](https://community.hivemq.com/u/AaronTLFranz)\
**Post date:** [August 19, 2024, 4:00pm UTC](https://community.hivemq.com/t/ios-swift-cloud-hivemq-example/933/11 "2024-08-19T16:00:04Z")

</div>

Hello @mgs227 ,

Thank you for the follow-up!

Reviewing some of the documentation for [CocoaMQTT](https://github.com/emqx/CocoaMQTT), this sounds like it will be able to connect to HiveMQ instances without issue, as there is support for TLS connectivity.

It also appears that, in their included examples, they do offer a selection that provides an example of [TLS implementation](https://github.com/emqx/CocoaMQTT/blob/master/Example/Example/ViewController.swift).

I would note that this is required for HiveMQ Cloud connectivity. Cloud Starter users can set up mTLS directly via the Access Management portal of their cloud instance, which can be used to provide the required certs for this library. Otherwise, one-sided TLS authentication can be enabled by ensuring the HiveMQ Server cert (available [here](https://hivemq.atlassian.net/l/cp/mSmww9yZ)) is trusted, or by trusting currently untrusted certificates.

`mqtt.allowUntrustCACertificate = true`

Best,  
Aaron from the HiveMQ Team
