This article takes you through the steps that are required to get iOS applications to exchange messages with an MQTT server. After you master those steps, you can begin to include mobile messaging in your iOS applications and gain the advantages of reliable mobile app connectivity to the enterprise. For use on iOS devices (that is, iPhone and iPad), you must build the MQTT client library for C from the source code that is provided as part of this exercise.
Before you begin
- Link to the iOS Dev Center and know how to develop applications for iOS.
- Obtain an Apple Mac with OS X 10.8.2, or later to run the Xcode integrated development environment (IDE).
- (Optional) Configure a C development environment on Windows or Linux. It is useful to build and run the MQTT client sample C apps on Windows or Linux before you develop an MQTT iOS app. Alternatively, study the sample source code without building the samples.
- For supported and reference MQTT client platforms for C, see System requirements for IBM® Mobile Messaging & M2M Client Pack MA9B (superseded).
- If there is a firewall between your client and the server, check that it does not block MQTT traffic.
About this task
The procedure guides you through the following steps:
- Learn about programming for MQTT by studying, building, and running the MQTT client sample apps and MQTT client libraries for C.
- Install the Xcode development environment for iOS on Apple Mac.
- Do the task Building the MQTT client for C libraries to build the MQTT client for C libraries for iOS devices.
- Choose an MQTT server to which you can connect the client app.
The server must support the MQTT version 3.1 protocol. All MQTT servers from IBM do this, including IBM® MQ and IBM MessageSight. See Servers for Mobile Messaging & M2M.
- Download the IBM Messaging Telemetry Clients SupportPac and install the MQTT SDK.
There is no installation program, you just expand the downloaded file.
- Download the IBM Messaging Telemetry Clients SupportPac, or download the clients and libraries under an open source license from the Eclipse Paho project.
- Create a folder where you are going to install the SDK.
You might want to name the folder MQTT. The path to this folder is referred to here as sdkroot.
- Expand the compressed IBM Messaging Telemetry Clients SupportPac file contents into sdkroot. The expansion creates a directory tree that starts at sdkroot\SDK.
- Optional: Familiarize yourself with the MQTT API by studying the MQTT client sample C apps.
- Build the synchronous MQTT client sample C app MQTTV3sample.c for Windows or Linux. See Get up and running with Mobile Messaging & M2M in C.
- Connect to an MQTT version 3 server, and publish and subscribe to topics on the server.
- Study the source code and MQTT API documentation. For links to client API documentation for the MQTT client libraries, see MQTT client programming reference.
Learn how to create and resume MQTT clients, and publish and subscribe to MQTT topics, by studying the synchronous sample. The synchronous sample is simpler than the asynchronous sample. If you have not programmed MQTT before, write a synchronous MQTT program to become familiar with the MQTT programming model and API.
- Build the asynchronous MQTT client library for C on Windows or Linux. See Building the MQTT client for C libraries.
- Build and run the asynchronous MQTT client sample publish and subscribe C app.
- Study the MQTT client sample asynchronous C app source code and MQTT reference documentation.
You must use the asynchronous interface to write an MQTT application for a mobile device. Well-written apps that call the asynchronous interface are more responsive and stretch battery life further than apps written to the synchronous interface.
The asynchronous interface has two degrees of asynchronicity:
- The first degree is to unblock the application while the MQTT client library waits for publications from the server.
- The second degree is to unblock the application while the client library connects to the server, creates subscriptions, and posts publications.
- Download and install the iOS development tools.
If the Mac app store offers multiple versions of the simulator, choose the version that is compatible with the level of iOS you are targeting for your app.
- Log on with a user ID that has administrative privileges.
- Check your Apple Mac is at version 10.8.2 or later.
- Go to the website Xcode to download Xcode from the Mac app store.
- Install Xcode, the command-line environment, and the simulator.
- Build the MQTT client libraries for C on iOS. See Building the MQTT client for C libraries.
What to do next
- Verify the MQTT client libraries for C that you built:
- Use the Xcode development environment to compile the asynchronous MQTT client sample C app, and link to the unsecured asynchronous MQTT client library for C.
- Use the Xcode development environment to run the asynchronous MQTT client sample C app on an iOS device. Connect the sample to the MQTT version 3 server you configured; see Configuring the MQTT service from the command line.
- Create an MQTT client C app for iOS. The coding examples for the asynchronous C application, might prove helpful. The examples are MQTTV3ASample.c and MQTTV3ASSample.c in sdkroot\SDK\clients\c\samples. As an exercise, start by implementing the MQTT publish/subscribe sample.