This project showcases different transport formats and libraries to consume web services from an iPhone application. It features a server application, a command-line script that saves the output of the server application in different formats, and an iPhone client application (compatible with iPhone OS 3.0 and higher).
The server application (written in PHP) reads a MySQL database (Server/data/schema.sql) and outputs data in the following formats:
- HTML
- JSON
- YAML
- XML (arbitrary format)
- SOAP
- Property list
- Binary property list
- XML property list
- CSV
- Protocol Buffer (Google)
For each output format, there is a single “Formatter” subclass in the Server/formatters folder. A static method in the FormatterFactory class returns the formatter that corresponds to the requested output format.
In future versions of this project, the following formats and libraries could be integrated:
- MessagePack
- Apache Thrift (StackOverflow article about how to integrate with the iPhone)
- BERT
- Apache Avro
- Open Network Computing Remote Procedure Call (ONC RPC) aka Sun RPC
The project uses several different libraries:
For the server:
- cfpropertylist – A PHP Implementation Of Apple’s plist (PropertyList).
- NuSOAP – SOAP Toolkit for PHP.
- spyc – A simple YAML loader/dumper class for PHP.
- pb4php – Protocol Buffer for PHP
For the iPhone client:
- AFNetworking – a very fast, NSOperation-based networking library
- NextiveJSON – a very fast JSON parsing library
- ASIHTTPRequest – wrapper around the CFNetwork API, no longer maintained
- json-framework – A strict JSON parser/generator for Objective-C.
- JSONKit – A Very High Performance Objective-C JSON Library.
- YAJL-objc – Objective-C bindings for the YAJL parser.
- BSJSON – A simple JSON parser in Objective-C.
- touchcode – Repository of iPhone and iPod Touch source code.
- cCSVParse – A CSV parsing library in Objective-C.
- TBXML V1.3 – XML Parser by Tom Bradley.
- KissXML – KissXML is a replacement for Cocoa’s NSXML cluster of classes. It is based on libxml.
- TinyXML – TinyXML is a simple, small, C++ XML parser that can be easily integrating into other programs.
- gdata-objectivec-client – XML support classes included in the Google Data APIs Objective-C Client Library.
- APXML – NSXMLDocument ’substitute’ for iPhone/iPod Touch.
- ProtocolBuffers – Objective-C Protocol Buffers for OSX and the iPhone.
- AQXMLParser – Another parser for XML in Objective-C.
- InAppSettingsKit – Library allowing to access the current setting bundle from the application.
For parsing YAML in Cocoa, the code in this page was adapted, removing dependencies on the Cocoa AppKit framework (not available on the iPhone).
If you want to compile the YAML::Syck library with Fink, follow these steps:
- Install Fink on Mac OS X 10.6 from source following these instructions (there is no binary installer available for Snow Leopard at the time of this writing).
- Type “/sw/bin/fink install syck” to install syck.
- The Xcode project should now compile without problems.
To avoid these steps above, this project includes a version of libsyck.a already compiled as a “fat binary” format for both ARM (iPhone OS) and Intel (Simulator) processors.
The wsdl2objc tool was used to generate Objective-C (Cocoa) code from the server’s WSDL. This code was later adapted to parse the array returned by the server.
- Set up the server:
- Upload the “Server” folder contents via FTP or other means.
- Create a MySQL database.
- Import the “Server/data/schema.sql” file into the database. This will create a “data” table in the database.
- Change the settings in the “Server/data/database.php” file to access the database (server, username, password).
- Set up the client:
- Open the “Client/Client.xcodeproj” project in Xcode.
- Compile the project.
- Execute the client, which will connect to your server and retrieve data.
- If required, change the URL of the remote data tapping the “Settings” button in the interface.
- The sample data generated in this example comes from http://www.generatedata.com/.
- cfpropertylist in Ruby – Read, manipulate and write both binary and XML property lists as defined by Apple.
- PyYAML – PyYAML.org – the home of various YAML implementations for Python.
- plistlib – Generate and parse Mac OS X .plist files from Python.
- Discussion about CSV parsing in Objective-C
- soapUI – Cross-platform tool for testing SOAP web services.
- How To Choose The Best XML Parser for Your iPhone Project – by Ray Wenderlich, iPhone Developer and Gamer.
- iPhone Programming Tips: building Unix software – The bundled libsyck.a static library (fat binary for both ARM and Intel) was created using the information in this page.
- XML Benchmarks – libxml2 sax
- Using the SAX Interface of LibXML
- libxml2 Push Parsing
- web services with cocoa surprise
- iphone plist tutorial
- Comparison of data serialization formats
- Property list – Wikipedia page.
- Protocol Buffers – Wikipedia page.
- Protocol Buffers Developer Guide
- XMLRPC framework used by the WordPress iPhone application.
- AQXMLParser == Big Memory Win – Article about the AQXMLParser library.
- Results of Northwind database rows serialization benchmarks run at 06/02/2010 – Interesting analysis of serialization performance using .NET libraries.
The application icon and splash screen have been adapted from this picture.
This project is released with a liberal BSD license. See the LICENSE file for details.