Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Kudos

Introduction

 



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

<br />



The developed application retrieves data from an SAP system calling a function module exposed as a web service.

 

 



!https://weblogs.sdn.sap.com/weblogs/images/252152730/overview.png|height=268|alt=image|width=535|src...!

 

 



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.

 

 




!https://weblogs.sdn.sap.com/weblogs/images/252152730/app.jpg|height=483|alt=image|width=255|src=http...!

 

 



In order to build and run the
application the following activities must be performed:



  • Generate
    proxy code from the web service definition file
  • Integrate
    the generated code in your iPhone project
  • Build
    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:

 

Request:



+ +</p>


<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>


<p>  
<soapenv:Header/></p>


<p>  
<soapenv:Body></p>


<p>     
<urn:BupaGetdetails></p>


<p>        
<IvPartner>0000000001</IvPartner></p>


<p>     
</urn:BupaGetdetails></p>


<p>  
</soapenv:Body></p>


<p></soapenv:Envelope></p>


<p>+ +



Response:



+ +</p>


<p><soap-env:Envelope
xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"></p>


<p>   <soap-env:Header/></p>


<p>  
<soap-env:Body></p>


<p>     
<n0:BupaGetdetailsResponse
xmlns:n0="urn:sap-          com:document:sap:soap:functions:mc-style"></p>


<p>        
<EtBupaDetails></p>


<p>           
<Partner/></p>


<p>           
<NameLast>Rossi</NameLast></p>


<p>           
<NameFirst>Mario</NameFirst></p>


<p>           
<Namemiddle/></p>


<p>           
<Birthdt>1982-11-11</Birthdt></p>


<p>        
</EtBupaDetails></p>


<p>     
</n0:BupaGetdetailsResponse></p>


<p>  
</soap-env:Body></p>


<p></soap-env:Envelope></p>


<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.

 

<br />



!https://weblogs.sdn.sap.com/weblogs/images/252152730/sudzcj.png|height=207|alt=image|width=630|src=h...!

 

 

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
format



-      
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.

In the
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
following way:



Open the file /Source/Soap/Soap.m and, in the second method +createEnvelope, +replace the following line:



 



+[s appendFormat: @"

+[s appendFormat:
@"<urn:%@>%@</urn:%@>", method,[params
stringByReplacingOccurrencesOfString:@"&"
withString:@"&amp;"], method];+



 



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
documentation (/Documentation/index.html).



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
syntax:



 

result.<tagname>



 



where <tagname>
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!

 

 

 

 

<br /></p>
3 Comments