Escolar Documentos
Profissional Documentos
Cultura Documentos
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.
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
3- Coding
Create an interface like this :
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 >
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)