Remoting on The iPhone

Thanks to Apple’s excellent support of C++ on iPhone OS, Remoting is working fine on the iPhone, iPod Touch and iPad. The hardest thing when building a Remoting-based iPhone application is to integrate the necessary C++ libraries into the Xcode project. This article shows how to build an iPhone client that works with the Remoting-based Stock Quote Server sample shown in our first Remoting Screencast.
The first step is configuring and building the POCO C++ Libraries and Remoting framework for the iPhone (and, optionally, the iPhone Simulator). The POCO C++ Libraries and Remoting source code distribution come with a build configuration for building static libraries for the iPhone. Note that shared libraries are not supported on the iPhone, as there is no way to distribute them via the iPhone App Store. A standard POCO C++ Libraries distribution contains a few libraries that cannot be used on the iPhone, due to a lack of required third-party libraries. These are the Data/ODBC and Data/MySQL libraries. The Crypto and NetSSL_OpenSSL libraries require OpenSSL static libraries, which are not included in the standard iPhone SDK. If required, they can be built from the OpenSSL sources, though. On the iPhone, we do not need the Remoting code generator (it cannot run there anyway), so we leave it out as well. So we end up with the following steps to configure and build the libraries:

$ cd poco-2009.2p4
$ ./configure --config=iPhone --no-samples --no-tests
$ make -s -j4

If required, we can also build the libraries for the iPhone Simulator, by simply using the iPhoneSimulator build configuration instead of the iPhone build configuration. Note that the iPhoneSimulator build configuration is only available with release 2009.2p4 or later.

$ ./configure --config=iPhoneSimulator --no-samples --no-tests
$ make -s -j4

Now that the static libraries for the iPhone have been built, we can start building the iPhone application with Xcode. For this sample, we simply create a new iPhone application project based on the View-based Application template.
After creating the template, the first step is to add the necessary POCO and Remoting libraries to the project. This can be simply done by dragging the libraries from the poco-2009.2p4/lib/iPhoneOS/armv6 directory into the project’s Frameworks folder.


The next step is setting the header file and library search paths in the project’s settings.


For the library search paths, we want two separate search paths, one to be used when building for the iPhone, and another one to be used when building for the iPhone Simulator. This can be done by using build setting conditions in the project settings window.




After the search paths have been set up, we can start with the code. First, we need to register the Remoting Binary transport with the Remoting ORB. We do this in our application’s main() function. The main() function is implemented in file main.m. Since we’re going to mix C++ with Objective-C, we change the file’s extension to .M, which tells the compiler to assume Objective-C++ as source language.


Next, we add the Remoting client files generated by the Remoting code generator to the project. We simply use the files generated for the original client application — see the <"">screencast for how to generate these files. Simply drag the header and source files into the project, and let Xcode copy them into the project directory.


We can now work on the application’s user interface. The application has a text field for entering a stock symbol, a button for sending a quote request for the symbol to the Remoting server, and a label for displaying the result. We also add the necessary actions and outlets to the view controller.


The final thing to implement is the getQuote() method in the view controller. This is the method that will use Remoting to send the stock quote request to the server. The important thing in this method is to catch all POCO-based exceptions, to avoid C++ exceptions spilling into the Cocoa framework. The necessary conversions from std::string to NSString and vice-versa are straightforward.


What’s left to do is some code to handle the text field input, and a few other things typical for iPhone applications. Please note that the file extension for the source file containing the controller has been changed to .M, as the file contains Objective-C++ code.

The final application can be seen below.


The complete source code for this application can be downloaded here. The source code for the server and command-line client can be downloaded here and here.