Merge Objective-C static library into Swift (using mosquitto)

Sam Wang
Sam Wang
Nov 19, 2014 · 4 min read

Swift is an awesome language, it’s safe, it’s simple and easy learning, but sometimes we still need to use the existed project which written in Objective-C previously, how should we do at this moment?

That’s take the project I created for mosquitto client as an example. (https://github.com/samwang0723/libmosquitto)

Mosquitto & MQTT

“MQTT is a machine-to-machine (M2M)/”Internet of Things” connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium.”

MQTT is a milestone of The Internet of Things (IoT), with MQTT, you can create an instant message App, communicate with terminal devices, Home automation, etc… MQTT’s subscribe/publish mechanism which saves the network traffic and power, not like traditional Socket (persistent connection), MQTT using ping to detect the client and transmit message when existed.

Mosquitto is an open source (BSD licensed) message broker that implements the MQ Telemetry Transport protocol versions 3.1 and 3.1.1., with Mosquitto, developers can easily establish the environment on their computers/servers.

Install Mosquitto

There are many ways to install mosquitto on your computer, in this article I use MacOS, so I will install it through “brew install mosquitto”, you can follow the Binary Installation instruction here: http://mosquitto.org/download/

After installation complete, you will see there is an executable “mosquitto” on /usr/local/Cellar/mosquitto/(version)/sbin, execute it:

mosquitto will use default configuration and bind with port 1883 as a MQTT broker.

Build libmosquitto

Download the project from my github and build, you will have three headers and one static library(universal build already has all architectures of iOS CPU), these contains the main function of mosquitto client, we have to import it into our Swift project.

Bridging Header

In WWDC 2014 tutorial video session, we understand that Swift code can be embed into Objective-c project, and Objective-c can also include with Swift project. https://developer.apple.com/videos/wwdc/2014/?id=402

Reference from above picture, we realized the bridge header is needed, there are three headers for libmosquitto right now, MosquittoClient.h, MosquittoMessage.h and mosquitto.h, and I am going to bridge them into Mosquitto-Bridging-Header.h (File->New->File->Header file)

Next we are going to make sure the Build Phases contains above headers in our Swift project.

In Build Settings, add bridging header file location into Swift Compiler

Code Implementation

After all settings are done, let’s create a instance of MosquittoClient:

See! this sample use MosquittoClient without import anything. Bridging header helps to transform Objective-c into Swift format, even if the delegate.

If we implemented the MosquittoClientDelegate, press “esc” to list the candidate of functions and you can see all delegate methods in Swift format.

Alright, now is the time to complete the code, create MosquittoClient with certain ID, set connection target to 127.0.0.1 on port 1883 (these parameters depends on your MQTT broker environment), then connect().

Then, in didConnect callback, we subscribe certain topic(“sample”) if connect success.

After launch on simulator or actual iPhone/iPad, you can see the connection established information from terminal or Xcode debug console.

Next we use mosquito_pub to publish message with certain topic.

./mosquitto_pub -t “sample” -m “hello swift” -q 1

App can received message from callback

didReceiveMessage, msg=hello swift

Conclusion

This tutorial shows how to reuse the Objective-c code/library with Swift project, Apple doesn’t want developer to abandon the Objective-c project at all, and you don’t have to rewrite all things, just use bridge header to achieve your goal.

Swift Programming

The Swift Programming Language

    Sam Wang

    Written by

    Sam Wang

    Engineer, Guitarist, Music Lover

    Swift Programming

    The Swift Programming Language

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade