You are on page 1of 5

Articles Mobile Development iPhone General

iOS Soap Webservice Calling and Parsing the Response


By Safak Tarazan, 19 Jul 2013
0.00 (No votes)

Download source code - 599.4 KB

Introduction
As you know, web services are widely used to make applications to communicate each other. In this article, I will show how to call an ASMX service in iOS. Simply, we will send a Celsius value to the server and the server will convert it to Fahrenheit.

Background
It is assumed that the reader already has knowledge about Objective C, Xcode, and general Webservice structure.

Using the code


What is WebService?
Simply, a webservice is a method that allows devices to communicate with each other.

How is this happening?


Very simply, there are some methods on the server side and the client reaches those methods by sending a request. This request may contain some parameters for the methods. After the server performs the action, it returns the answer. But how? SOAP objects...

SOAP Object?

SOPA is a protocol which allows to share information with a certain format. Actually it is a type of XML. Now we can start our application. Here are the steps: Download ASIHTTPRequest framework Download JSON framework Add other needed frameworks Coding

1- Download ASIHTTPRequest
Go to the following link and get your framework; after downloading, copy all the files under the "classes " folder to your project framework folder. ASIHTTPRequest

2- Download JSON framework


Go to the following link and get your framework; after downloading, copy all the files under the src/main/obj folder to your project framework folder.

JSON 3- Add other needed frameworks


From Project Explorer select your project and go to the Build Phases tab, then click Link Binary With Libraries, click + sign, and find the following frameworks and add them. You will see that, they will be added to your project but it is better to move them to your project framework folder: CFNetwork.framework SystemConfiguration.frameworkMobileCoreServices.framework libz.1.2.3.dylib

3- Coding
Create an interface like this :

Import the frameworks header to your ViewController header file :


# i m p o r t" A S I H T T P R e q u e s t . h " # i m p o r t" A S I F o r m D a t a R e q u e s t . h " # i m p o r t" S B J s o n . h "

Unfortunately, there is no framework to create a SOAP message for us, so we will create the SOAP message as follows (this is the routine) :
-( I B A c t i o n ) b t n C o n v e r t : ( i d ) s e n d e r{ N S S t r i n g* s o a p M e s s a g e=[ N S S t r i n gs t r i n g W i t h F o r m a t : @ " < ? x m lv e r s i o n = \ " 1 . 0 \ " e n c o d i n g = \ " u t f 8 \ " ? > \ n " " < s o a p : E n v e l o p ex m l n s : x s i = \ " h t t p : / / w w w . w 3 . o r g / 2 0 0 1 / X M L S c h e m a i n s t a n c e \ " x m l n s : x s d = \ " h t t p : / / w w w . w 3 . o r g / 2 0 0 1 / X M L S c h e m a \ " x m l n s : s o a p = \ " h t t p : / / s c h e m a s . x m l s o a p . o r g / s o a p / e n v e l o p e / \ " > \ n "

" < s o a p : B o d y > \ n " "< C e l s i u s T o F a h r e n h e i tx m l n s = \ " h t t p : / / t e m p u r i . o r g / \ " > \ n " " < C e l s i u s > % @ < / C e l s i u s > \ n " " < / C e l s i u s T o F a h r e n h e i t > \ n " " < / s o a p : B o d y > \ n " " < / s o a p : E n v e l o p e > \ n ", t e x t F i e l d C e l c i s u s . t e x t ] ; N S U R L* u r l=[ N S U R L U R L W i t h S t r i n g : @ " h t t p : / / w 3 s c h o o l s . c o m / w e b s e r v i c e s / t e m p c o n v e r t . a s m x " ] ; N S M u t a b l e U R L R e q u e s t* t h e R e q u e s t=[ N S M u t a b l e U R L R e q u e s tr e q u e s t W i t h U R L : u r l ] ; N S S t r i n g* m s g L e n g t h=[ N S S t r i n gs t r i n g W i t h F o r m a t : @ " % d " ,[ s o a p M e s s a g el e n g t h ] ] ; [ t h e R e q u e s ta d d V a l u e :@ " t e x t / x m l ;c h a r s e t = u t f 8 "f o r H T T P H e a d e r F i e l d : @ " C o n t e n t T y p e " ] ; [ t h e R e q u e s ta d d V a l u e :@ " h t t p : / / t e m p u r i . o r g / C e l s i u s T o F a h r e n h e i t " f o r H T T P H e a d e r F i e l d : @ " S O A P A c t i o n " ] ; [ t h e R e q u e s ta d d V a l u e :m s g L e n g t hf o r H T T P H e a d e r F i e l d : @ " C o n t e n t L e n g t h " ] ; [ t h e R e q u e s ts e t H T T P M e t h o d : @ " P O S T " ] ; [ t h e R e q u e s ts e t H T T P B o d y :[ s o a p M e s s a g ed a t a U s i n g E n c o d i n g : N S U T F 8 S t r i n g E n c o d i n g ] ] ; N S U R L C o n n e c t i o n* t h e C o n n e c t i o n= [ [ N S U R L C o n n e c t i o na l l o c ]i n i t W i t h R e q u e s t : t h e R e q u e s td e l e g a t e : s e l f ] ; i f (t h e C o n n e c t i o n) { w e b D a t a=[ N S M u t a b l e D a t ad a t a ]; N S L o g ( @ " P r o b l e m " ) ; } e l s e { N S L o g ( @ " t h e C o n n e c t i o ni sN U L L " ) ; } } / /e n do fb u t o nc o n v e r t

After making our request to the server, we need to catch the server response, for this. By using the c o n n e c t i o n D i d F i n i s h L o a d i n gmethod, we catch the service response.
( v o i d ) c o n n e c t i o n : ( N S U R L C o n n e c t i o n* ) c o n n e c t i o nd i d R e c e i v e R e s p o n s e : ( N S U R L R e s p o n s e * ) r e s p o n s e { [ w e b D a t as e t L e n g t h :0 ] ; } ( v o i d ) c o n n e c t i o n : ( N S U R L C o n n e c t i o n* ) c o n n e c t i o nd i d R e c e i v e D a t a : ( N S D a t a* ) d a t a { [ w e b D a t aa p p e n d D a t a : d a t a ] ; } ( v o i d ) c o n n e c t i o n : ( N S U R L C o n n e c t i o n* ) c o n n e c t i o nd i d F a i l W i t h E r r o r : ( N S E r r o r* ) e r r o r { N S L o g ( @ " E R R O Rw i t ht h e C o n e n c t i o n " ) ; } ( v o i d ) c o n n e c t i o n D i d F i n i s h L o a d i n g : ( N S U R L C o n n e c t i o n* ) c o n n e c t i o n { N S L o g ( @ " D O N E .R e c e i v e dB y t e s :% d " ,[ w e b D a t al e n g t h ] ) ; N S S t r i n g* t h e X M L=[ [ N S S t r i n ga l l o c ]i n i t W i t h B y t e s : [ w e b D a t am u t a b l e B y t e s ]l e n g t h : [ w e b D a t al e n g t h ]e n c o d i n g : N S U T F 8 S t r i n g E n c o d i n g ] ; }

Now we got the data in XML form, but we need to parse this XML to get our fahrenheit value. To do that, we need to add N S X M L P a r s e r 's delegate to our viewController:
@ i n t e r f a c eS E V i e w C o n t r o l l e r:U I V i e w C o n t r o l l e r< N S X M L P a r s e r D e l e g a t e >

In c o n n e c t i o n D i d F i n i s h L o a d i n gmethod, we create an N S X M L P a r s e robject and pass the server response to it:


( v o i d ) c o n n e c t i o n D i d F i n i s h L o a d i n g : ( N S U R L C o n n e c t i o n* ) c o n n e c t i o n { N S L o g ( @ " D O N E .R e c e i v e dB y t e s :% d " ,[ w e b D a t al e n g t h ] ) ; N S S t r i n g* t h e X M L=[ [ N S S t r i n ga l l o c ]i n i t W i t h B y t e s : [ w e b D a t am u t a b l e B y t e s ]l e n g t h : [ w e b D a t al e n g t h ]e n c o d i n g : N S U T F 8 S t r i n g E n c o d i n g ] ; N S D a t a* o s m a n=[ t h e X M Ld a t a U s i n g E n c o d i n g : N S U T F 8 S t r i n g E n c o d i n g ] ; N S X M L P a r s e r* x m l P a r s e r=[ [ N S X M L P a r s e ra l l o c ]i n i t W i t h D a t a : o s m a n ] ; / /D o n ' tf o r g e tt os e tt h ed e l e g a t e ! x m l P a r s e r . d e l e g a t e=s e l f ; / /R u nt h ep a r s e r B O O Lp a r s i n g R e s u l t=[ x m l P a r s e rp a r s e ] ; }

Finally, we will implement N S X M L P a r s e r 's delegate methods to get each element:


( v o i d )p a r s e r : ( N S X M L P a r s e r* ) p a r s e rd i d S t a r t E l e m e n t : ( N S S t r i n g* ) e l e m e n t N a m e n a m e s p a c e U R I : ( N S S t r i n g* ) n a m e s p a c e U R Iq u a l i f i e d N a m e : ( N S S t r i n g* ) q N a m ea t t r i b u t e s : ( N S D i c t i o n a r y* ) a t t r i b u t e D i c t { N S S t r i n g * c u r r e n t D e s c r i p t i o n=[ N S S t r i n ga l l o c ] ; } -( v o i d ) p a r s e r : ( N S X M L P a r s e r* ) p a r s e rf o u n d C h a r a c t e r s : ( N S S t r i n g* ) s t r i n g { c u r D e s c r i p t i o n=s t r i n g ; } -( v o i d ) p a r s e r : ( N S X M L P a r s e r* ) p a r s e rd i d E n d E l e m e n t : ( N S S t r i n g* ) e l e m e n t N a m e n a m e s p a c e U R I : ( N S S t r i n g* ) n a m e s p a c e U R Iq u a l i f i e d N a m e : ( N S S t r i n g* ) q N a m e { i f ( [ e l e m e n t N a m ei s E q u a l :@ " C e l s i u s T o F a h r e n h e i t R e s u l t " ] ) ; t e x t F i e l d R e s u l t . t e x t=c u r D e s c r i p t i o n ; }

License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author