Software engineer, data guy, Open Source enthusiast, New Hampshire resident, husband, father. Fan of guitars, hiking, photography, homebrewing, sarcasm.
Drupal 6: Using Appcelerator Titanium to create an iPhone application to communicate with a Drupal site via SOAP (suds client & nusoap server)
In this article I’ll share some code that I wrote that uses Appcelerator Titanium to create a simple iPhone application that communicates with a Drupal site via SOAP. From the iPhone side of things I used the Suds Javascript SOAP client, and from Drupal, I created a nusoap soap server instance.
Let’s start with the SOAP server instance. I created a module called “nusoap”, and copied the lib folder from nusoap into it. I then created a file in the module’s directory called “soap-server.php” and added the following code. For the sake of this article, I did not include the rest of the Drupal module’s code.
The above code registers one soap server method “login_fetch_data” which accepts the arguments (name and pass), loads a full Drupal bootstrap, authenticates the user, and returns back the user’s uid. By browsing to the soap server file directly, I can see nusoap’s WSDL definition: (example: http://drupal.vm/sites/all/modules/custom/nusoap/soap-server.php)
To test if the soap server method was working, I used the follow PHP code snippet (which I pulled out of a test page callback):
I then created a new Titanium iPhone project. In the Resources directory, I created a directory called “soap-test-includes” to contain the suds.js library, and the soap.js file which uses this library. The suds.js file is available in the Kitchen Sink Titanium project, if you choose to import it.
I added the following code to the app.js file:
And I added the following code to the soap.js include file:
When I build and run the project, the iOS Simulator opens and shows the results of the soap connection!
As you can see, Titanium allows you to write an iPhone application without touching the native Objective-C code. In this example, all the code was written in javascript, with a PHP backend. Much of the javascript code I used is based on code found in the Kitchen Sink project; I suggest you check it out :)