Consuming SAP web services on iOS devices using WSDL 2 Obj C

fortunajugglerMobile - Wireless

Jul 19, 2012 (4 years and 9 months ago)

1,559 views



SAP COMMUNITY NETWORK


SDN
-

sdn.sap.com

|
BPX
-

bpx.sap
.com

|
BOC

-

boc.sap.com |
UAC

-

uac.sap.com

© 2011

SAP AG


1


Consuming SAP
W
eb
S
ervices on
iOS D
evices
using

WSDL2ObjC

Applies to:

Systems

running SAP NetWeaver
7.01, but should work on any other system providing a SAP Web Service
via SOAP.

For more information, visit the
Mobile homepage
.

Summary

This article describes
a quick way

to consume a
SOAP Web Service

provided by any
SAP NetWeaver

Sy
stem within native

applications developed for
iOS

devices (iPhone, iPad etc.).
The solution

is based on
the open
-
sourc
e
tool
WSDL2ObjC
, which generates ready
-
to
-
use object classes to be integrated in your
Cocoa

/ Cocoa Touch

application.


Author
:

Christian Klöppel

Company:

CSC Deutschland Solutions

GmbH

Created on:

26

January
20
1
1

Author Bio

Christian Klöppel works as Bu
siness Architect & Solution Manager at CSC Deutschland Solutions GmbH

(http://www.de.csc.com)

in Wiesbaden, Germany.
Besides working on web
-
based portal & content
management projects, he engages strongly in mobile applications & services.


Consuming SAP

Web Services on iOS Devices using WSDL2ObjC

SAP COMMUNITY NETWORK

SDN
-

sdn.sap.c
om

|
BPX
-

bpx.sap
.com

|
BOC

-

boc.sap.com |
UAC

-

uac.sap.com

© 2011

SAP AG


2


Table of Content
s

Introduction

................................
................................
................................
................................
.........................

3

Prerequisites

................................
................................
................................
................................
...................

3

Process Overview

................................
................................
................................
................................
...........

3

Assumptions

................................
................................
................................
................................
....................

3

Creating the Web Service Classes

................................
................................
................................
.....................

3

Getting the WSDL

................................
................................
................................
................................
...........

3

Using WSDL2ObjC

................................
................................
................................
................................
.........

4

Integrating the Web Service Classes into your Project

................................
................................
...................

5

Using the Web Service Class

................................
................................
................................
.............................

6

Preparing the Web Service Class

................................
................................
................................
...................

6

Adjust Method Names

................................
................................
................................
................................
..................

6

Adjust Namespace Prefixes

................................
................................
................................
................................
.........

6

Generating a Request

................................
................................
................................
................................
.....

7

Create Binding

................................
................................
................................
................................
.............................

7

Create Request

................................
................................
................................
................................
............................

7

Call Request

................................
................................
................................
................................
................................

7

Handling the Data

................................
................................
................................
................................
...........

8

Handling Exceptions

................................
................................
................................
................................
.......

8

Related Content

................................
................................
................................
................................
..................

9

Disclaimer and Liability Notice

................................
................................
................................
..........................

10



Consuming SAP Web Services on iOS Devices using WSDL2ObjC

SAP COMMUNITY NETWORK

SDN
-

sdn.sap.c
om

|
BPX
-

bpx.sap
.com

|
BOC

-

boc.sap.com |
UAC

-

uac.sap.com

© 2011

SAP AG


3


Introduction

This article shows how to consume a web service provided by any SAP NetWeaver System using the
corresponding

WSDL and the open source framework
WSDL2ObjC

to generate ready
-
to
-
use object classes
to be integrated in your Cocoa applications.


Prerequisites



A “
M
ac




Apple’s
XCode

development environment

&
iOS

SDK



A
SAP NetWeaver

System providing a
corresponding

web service via SOAP



WSDL2Obj
C
, available at
http://code.google.com/p/wsdl2objc/



Op
tional:

soapUI

for testing the web service (
http://www.soapui.org
)

Process Overview

The application WSDL2ObjC will take a WSDL file and generate ready
-
to
-
use Objective
-
C classes which
cover the whole communication proc
ess with the SOAP provider and allow to create and access XML
-
element
s

in the SOAP data as Objective
-
C objects.


Assumptions

It is assumed that you have already prepared the basic
fundamentals

of your development, such as



SAP NetWeaver

up & running



An
co
rresponding

SOAP web service
configured and published



XCode

installed



An
corresponding

development project

started



Objective
-
C
,
Cocoa

and
XML

skills



A working
network connection

between your development environment and the SAP system

Creating the Web Servi
ce Classes

Getting the WSDL

It is assumed that you already have created and published the
corresponding

SOAP web ser
vice (via ABAP
workbench, i.e.)
. You then should be able to get the WSDL
-
file (Web Service Description L
anguage)
out of
SAP’s SOAMANAGER aft
er your created an appropriate service configuration.

Save the WSDL
-
file to your
hard disk
.



Consuming SAP Web Services on iOS Devices using WSDL2ObjC

SAP COMMUNITY NETWORK

SDN
-

sdn.sap.c
om

|
BPX
-

bpx.sap
.com

|
BOC

-

boc.sap.com |
UAC

-

uac.sap.com

© 2011

SAP AG


4



Using WSDL2O
bj
C

After the WSDL
-
file was successfully extracted and saved on your hard disk, th
e following steps will generate

the
corresponding

classes to access

the web service:

1
.

Create a new folder on your hard disk in which the generated classes should be saved (WSDL2ObjC
won’t do that for you).

2
.

Run WSDL2ObjC.


3
.

Within the first field, select the WSDL
-
file you just saved.

4
.

Within the second field, select the new
ly created folder to save the generated classes.

5
.

“Add tag to service name…” can be left unchecked.

6
.

Click “Parse WSDL”.


You’ll find the new classes in the folder you created. The
folder contains

some helper classes (which are
generally
included
when usin
g WSDL2ObjC) and the main web service class, named
according

to your
WSDL
-
file

(here: REQUEST)
. Every class consists of a header file (*.h) and an implementation file (*.m).


Consuming SAP Web Services on iOS Devices using WSDL2ObjC

SAP COMMUNITY NETWORK

SDN
-

sdn.sap.c
om

|
BPX
-

bpx.sap
.com

|
BOC

-

boc.sap.com |
UAC

-

uac.sap.com

© 2011

SAP AG


5



Integrating the Web Service Classes into your Project

The newly created classes
can now be imported into your XCode development project:

1
.

Open XCode and your development project.

2
.

Create a new group named “webservices”.


3
.

Drag & Drop all
generated

classes
(main class & helpers) into

the new webservices group

within
XCode
.

4
.

When prompted,

tell XCode to copy the sources into your project
folder
.


Note:

If you plan to access
more than one

web service, the steps above have to be repeated for every single WSDL.
The helper classes only have to be imported once, so after these were imported alo
ng with the first WSDL, you
only need
to import
the main web service class further on.


Consuming SAP Web Services on iOS Dev
ices using WSDL2ObjC

SAP COMMUNITY NETWORK

SDN
-

sdn.sap.c
om

|
BPX
-

bpx.sap
.com

|
BOC

-

boc.sap.com |
UAC

-

uac.sap.com

© 2011

SAP AG


6


Using the Web Service Class

Preparing the Web Service C
lass

After importing the web service class to your project it might be necessary to do some minor adjustments:



Ad
just object and method names when using own namespace definitions in SAP (

/cscuam/…

, i.e.)



Adjust namespace prefixes within the web service class in case SAP does not recognize elements

Adjust Method N
ames

In some cases, you might be using names like

/c
scuam/someOp
e
ration


in your function modules.




When ex
porting the WSDL, SAP transform
s the

/


to

_
-

, which means the name above will change
to


_
-
cscuam_
-
someOperation


in your WSDL. WSDL2ObjC
takes these names without any changes, which
results in
having objects and methods containing

_
-

.


To
fix

this, you should do a search & replace on the whole main web service class to remove the




and get
clear names like

_cscuam_someOperation

.



Note:
You
only have to

adjust object and method names. T
ake care about leaving the XML
-
element definitions as they
are, containing the original

_
-


naming!

Adjust Namespace P
refixes

WSDL2bjC will strictly set namespace prefixes on any single element defined via the main web service class.
In some cases, the SA
P web service provider won’t recognize child elements inclu
ding the full namespace
prefix.
This can be solved by removing
the prefix

in the
appropriate

element definitions wit
hin the main web
service class.




Consuming SAP Web Services on iOS Devices using WSDL2ObjC

SAP COMMUNITY NETWORK

SDN
-

sdn.sap.c
om

|
BPX
-

bpx.sap
.com

|
BOC

-

boc.sap.com |
UAC

-

uac.sap.com

© 2011

SAP AG


7


Please refer to your application’s log file po
pulated by the SOAP binding to see the XML final
ly created by
your application.

Note:
Using soapUI for testing the XML could help identifying problems.

Generating a R
equest

We now implement the request used to call the web service. The following things are

assumed to be done
and will not be discussed in detail:

1
.

Import the main web service class in your implementation file using
#import
“REQUEST.h


2
.

Create an instance

method to cover the SOAP acti
vities:
-

(void)callSOAP;

Within the
(void)callSOAP

method, we

now implemen
t the SOAP request.

Create B
inding

//create web service binding

REQUESTBinding *binding = [[REQUESTBinding alloc] initWithAddress:@"<YOUR_WS_URL>"];

binding.logXMLInOut = YES;

binding.authUsername =
@”<YOUR_WS_USER>”;

binding.authPassword =
@”
<YOUR_WS_PASSWORD>”;

You’ll find the binding object definition class within the main web service class
. Any objects you need within
the web service class are prefixed with your web service name (= name of the WSDL).

Replace the following Strings:



<YOUR_WS_
URL>:

URL to the web service



<YOUR_WS_USER>:

Username to access the web service (if needed)



<YOUR_WS_PASS>:

Password to access the web service (if needed)

The attribute
binding.logXMLInOut

is set to
YES

in order to track all SOAP communication within the
a
pplication’s log file.

Create R
equest

// create web service request object

REQUEST__cscuam_requestGetDetail *request = [[REQUEST__cscuam_requestGetDetail alloc]
init];

request.IvRequestNumber = requestItem.requestListRequestNumber;

The
REQUEST
-
class provid
es an object type which covers the
corresponding

operation you want to call via
SOAP (here:
/cscuam/requestGetDetail
). After the request object
is

allocated and initiated, you can set
the attributes. In this case, we simply want to set the attribute
IvRequ
estNumber

to call detailed data for the
corresponding

number.

Call R
equest

// call the request

REQUESTBindingResponse *response = [binding
_
cscuam_requestGetDetailUsingParameters:request];



NSArray *responseBodyParts = response.bodyParts;


[binding relea
se];

[request release];


Consuming SAP Web Services on iOS Devices using WSDL2ObjC

SAP COMMUNITY NETWORK

SDN
-

sdn.sap.c
om

|
BPX
-

bpx.sap
.com

|
BOC

-

boc.sap.com |
UAC

-

uac.sap.com

© 2011

SAP AG


8


By calling the method
_cscuam_requestGetDetailUsingParameters

with the
request

object
passed

over,
the web service is called and we receive the answer capsuled within a new
REQUESTBindingResponse

object.

The
bodyParts
can be extract
ed out of this object into a new
NSArray
.

After having the
bodyParts
ready to use,
binding

and
request

should be released.

Handling the D
ata

Finally, we can walk through the
bodyParts
-
array and extract the relevant data:

//

transfer SOAP
-
data into own obje
cts

for (id bodyPart in responseBodyParts) {




// check if bodyParty is of
corresponding

type


if([bodyPart isKindOfClass:[REQUEST__cscuam_requestGetDetailResponse class]]) {





REQUEST__cscuam_requestGetDetailResponse *part =
(REQUEST__cscuam_requestGet
DetailResponse *)bodyPart;






// read child element
EsMainData


if ([part.EsMainData isKindOfClass:[REQUEST__cscuam_srequestDetail class]]) {








REQUEST__cscuam_srequestDetail *item = part.EsMainData;








requestItem.mainDataRequestType = item.Req
uestType;



requestItem.mainDataEMailRequester = item.EMailRequester;



requestItem.mainDataStatusProfile = item.StatusProfile;



[…]







//
read child element
EsUserData


if ([part.EsUserData isKindOfClass:[REQUEST__cscuam_srequser class]]) {








REQ
UEST__cscuam_srequser *item = part.EsUserData;








requestItem.userDataTitle = item.Title;



requestItem.userDataNameLast = item.NameLast;



requestItem.userDataNameFirst = item.NameFirst;



[…]

The code above walks through the main re
s
ponse element (ca
lled
/cscuam/requestGetDetailResponse
)
and
checks for
different Elements included in the response. The attributes of the different child elements are
transferred into our own
requestItem

which is used for

further processing of the data within the applicati
on.

Handling Exceptions

Handling SOAP
-
faults is also described within the WSDL2ObjC
-
wiki at

http://code.google.com/p/wsdl2objc/wiki/UsageInstructions
. When evaluating the bodyPart o
f the response
-
objects you have to check for a SOAP

fault object:

// check for SOAP fault

if ([bodyPart isKindOfClass:[SOAPFault class]]) {



// You can get the error like this:




tV.text = ((SOAPFault *)bodyPart).simpleFaultString;




continue;


Consuming SAP Web Services on iOS Devices using WSDL2ObjC

SAP COMMUNITY NETWORK

SDN
-

sdn.sap.c
om

|
BPX
-

bpx.sap
.com

|
BOC

-

boc.sap.com |
UAC

-

uac.sap.com

© 2011

SAP AG


9


Related
Content

Web Services Development in ABAP

Web Services with SAP Part 2: Web Service Creation

WSDL2ObjC Wiki & Usage Instructions


Enterprise Micro Application Technical User Guide

For more information, visit the
Mobile homepage


Consuming SAP Web Services on iOS Devices using WSDL2ObjC

SAP COMMUNITY NETWORK

SDN
-

sdn.sap.c
om

|
BPX
-

bpx.sap
.com

|
BOC

-

boc.sap.com |
UAC

-

uac.sap.com

© 2011

SAP AG


10


Disclaimer and Liability Notice

This document may discuss sample coding or o
ther information that does not include SAP official interfaces and therefore is not
supported by SAP. Changes made based on this information are not supported and can be overwritten during an upgrade.

SAP will not be held liable for any damages caused by u
sing or misusing the information, code or methods suggested in this document,
and anyone using these methods does so at his/her own risk.

SAP offers no guarantees and assumes no responsibility or liability of any type with respect to the content of this te
chnical article or
code sample, including any liability resulting from incompatibility between the content within this document and the material
s and
services offered by SAP. You agree that you will not hold, or seek to hold, SAP responsible or liable with

respect to the content of this
document.