Getting started with the MQTT client for C on iOS

Learn how to get iOS applications to exchange messages with an MQTT server. 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 the MQTT software development kit.

Before you begin

  1. Link to the iOS Dev Center and know how to develop applications for iOS.
  2. Obtain an Apple Mac with OS X 10.8.2, or later to run the Xcode integrated development environment (IDE).
  3. (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.
  4. For supported and reference MQTT client platforms for C, see System requirements for IBM® Mobile Messaging and M2M Client Pack.
  5. 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:

  1. Learn about programming for MQTT by studying, building, and running the MQTT client sample apps and MQTT client libraries for C.
  2. Install the Xcode development environment for iOS on Apple Mac.
  3. Do the task Building the MQTT client for C libraries to build the MQTT client for C libraries for iOS devices.

Procedure

  1. 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 WebSphere® MQ and IBM MessageSight. See Getting started with MQTT servers.

  2. Download the Mobile Messaging and M2M Client Pack and install the MQTT SDK.
    There is no installation program, you just expand the downloaded file.
    1. Download the Mobile Messaging and M2M Client Pack.
    2. 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.

    3. Expand the compressed Mobile Messaging and M2M Client Pack file contents into sdkroot. The expansion creates a directory tree that starts at sdkroot\SDK.
  3. Optional: Familiarize yourself with the MQTT API by studying the MQTT client sample C app.
    1. Build the synchronous MQTT client sample C app MQTTV3sample.c for Windows or Linux. See Getting started with the MQTT client for C.
    2. Connect to an MQTT version 3 server, and publish and subscribe to topics on the server.
    3. 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.

    4. Build the asynchronous MQTT client library for C on Windows or Linux. See Building the MQTT client for C libraries.
    5. Build and run the asynchronous MQTT client sample publish and subscribe C app.
    6. 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:
      1. The first degree is to unblock the application while the MQTT client library waits for publications from the server.
      2. The second degree is to unblock the application while the client library connects to the server, creates subscriptions, and posts publications.
  4. Download and install the iOS development tools.
    1. Log on with a user ID that has administrative privileges.
    2. Check your Apple Mac is at version 10.8.2 or later.
    3. Go to the website Xcode to download Xcode from the Mac app store.
    4. Install Xcode, the command-line environment, and the simulator.
    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.
  5. Build the MQTT client libraries for C on iOS. See Building the MQTT client for C libraries.

What to do next

  1. Verify the MQTT client libraries for C that you built:
    1. 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.
    2. 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.
  2. 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.
    Tip: To get an idea of what the app might look like and do, look at screen captures of the MQTT client sample C app. See Getting started with the MQTT client for Java on Android.