Consuming XML Web Services in iPhone Applications
In this blog I am going to present an easy way to call ABAP code from a
native iPhone application using automatically generated proxy code from web
service definition files (WSDL files).
I have been working with SAP and ABAP code for four years and I have
recently started developing iPhone applications, so the first thing I naturally
tried to do was to put these two worlds in communication.
My first idea was to call an ABAP function module exposed as a web
service but as soon as I started developing my iPhone code, I realized this is
not a trivial task in objective-c programming language. Since there are not
standard libraries to interact with web services, the code to connect to the web server and to manipulate xml messages must be manually written. However xml
request and response messages are often very complex and the manual parsing
activities can be difficult and long lasting.
Fortunately, surfing the web, I discovered some tools that can automatically
generate proxy code from web service definition files, allowing
the code developer to really speed up his development activities.
In this blog I am going to show how to use one of these tools and how the
generated code can be integrated in your native iPhone application. The blog is
intended for people who are already familiar with the iPhone development
environment and the objective-c language, so I will focus only on the most
interesting code sections in terms of
web service consuming.
The iPhone Application
The developed application retrieves data from an SAP system calling a function module exposed as a web service.
The function module exposed for
testing purposes is BUPA_GETDETAILS. This function module has in input the Business Partner
number and in output the details of the selected Business Partner (e.g.
first name, last name, birthdate, etc.).
Using the developed
iPhone application the Business Partner number can be specified and the first
name and last name can be retrieved calling the web service.
In order to build and run the
application the following activities must be performed:
proxy code from the web service definition file
the generated code in your iPhone project
and run the iPhone application
*Expose the function module
as a web service*
In order to expose the
function module as a web service and to retrieve its web service definition
follow the steps reported in this Wiki.
At the end of the process the service Z_BP_GET_DETAILS
will be available with the corresponding web service definition file z_bp_get_details.wsdl, whose example request and
response message are:
<p><soapenv:Envelope</p><p> xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/</p><p> xmlns:urn=”urn:sap-com:document:sap:soap:functions:mc-style”></p>
*Generate proxy code from the
web service definition file*
– Generate proxy code
The second step is to automatically generate the
classes to interact with the web server, build the request message and parse the response message. In order
to do this I will use SUDZC , a web tool
for generating proxy code from web service definition files.
In order to generate the code, upload the WSDL file, specify a prefix for your classes (e.g. SDZ), select the type +Objective-C
for iPhone (alpha)+ and click on Generate.
A zip archive is created containing
the following directories:
Documentation: the documentation is designed to
allow you to reference the generated code and it is in an easy-to-read html
Examples: a simple project is created containing real code samples
Source: this directory contains the proxy generated code
WSDL: this directory contains the WSDL file previously uploaded
The zip archive created from the z_bp_get_details.wsdl file can be found here.
A real example of how to invoke the generated code in your project is
contained in the file /Source/Examples/SDZserviceExample.m.
+SDZservice service = ;*</p><p><strong>service.logging = YES;</strong></p><p>*service.username = @”username”;*</p><p><strong>service.password = @”password”;</strong></p><p>*// Returns SDZWfmcassBupaGetdtl. +
+[service BupaGetdetails:self +action:@selector(BupaGetdetailsHandler:) IvPartner: @””];
As you can see, you do not need to create the request
message, but you only need to invoke the method +BupaGetdetails +of the
object service, passing the IvPartner parameter. The name of the parameter is equal to the tag name in the xml request message.
following paragraphs I will show you how to manage the response message in the method BupaGetdetailsHandler.
– Modify the generated code
In order to be able to use the generated code with your SAP
web service, it is necessary to modify one of the generated files in the
Open the file /Source/Soap/Soap.m and, in the second method +createEnvelope, +replace the following line:
+[s appendFormat: @”
The modification simply consists in adding the “urn:” prefix
in the tag name definition.
As soon as you have done this modification, your generated
code is ready to be used in your project.
Integrate the generated code in your iPhone project
In order to use the generated code refer to the+ Adding the
source files to your project+ and How to use generated code sections in the
The following code shows how the service is invoked in the application and
how the xml response message is easily parsed.
The response message is represented by the +value
+parameter of id type of the method BupaGetdetailsHandler. The data contained in the response message
can be easily retrieved casting the message to an object result of type SDZWfmcassBupaGetdtl.
At this point you can retrieve your data using the following
is the name of the tag in the xml response message.
Build and run the iPhone application
Once the generated code has been integrated in your iPhone project, you are ready to build and run the application.
You can download the source code for this app here. Enjoy!