Click here to Skip to main content
13,193,551 members (50,061 online)
Click here to Skip to main content
Add your own
alternative version


12 bookmarked
Posted 19 Jul 2013

iOS Soap Webservice Calling and Parsing the Response

, 28 Jul 2013
Rate this:
Please Sign up or sign in to vote.
A simple application that communicates web service (SOAP).


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.


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.

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.

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 :

#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "SBJson.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) :

- (IBAction)btnConvert:(id)sender {
    NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
         "<soap:Envelope xmlns:xsi=\"\" 
         " <CelsiusToFahrenheit xmlns=\"\">\n"
         "</soap:Envelope>\n" ,textFieldCelcisus.text];
    NSURL *url = [NSURL URLWithString:@""];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue: @"" forHTTPHeaderField:@"SOAPAction"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLConnection *theConnection = 
      [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if( theConnection )
        webData = [NSMutableData data] ;
        NSLog(@"theConnection is NULL");
}// end of buton convert 

After making our request to the server, we need to catch the server response, for this. By using the connectionDidFinishLoading method, we catch the service response.

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    [webData setLength: 0];
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    [webData appendData:data];
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    NSLog(@"ERROR with theConenction");
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
    NSLog(@"DONE. Received Bytes: %d", [webData length]);
    NSString *theXML = [[NSString alloc] initWithBytes: 
      [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];

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 NSXMLParser's delegate to our viewController:

@interface SEViewController : UIViewController <NSXMLParserDelegate> 

In connectionDidFinishLoading method, we create an NSXMLParser object and pass the server response to it:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
    NSLog(@"DONE. Received Bytes: %d", [webData length]);
    NSString *theXML = [[NSString alloc] initWithBytes: 
      [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    NSData *myData = [theXML dataUsingEncoding:NSUTF8StringEncoding];
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:myData];
    // Don't forget to set the delegate!
    xmlParser.delegate = self;
    // Run the parser
    BOOL parsingResult = [xmlParser parse];

Finally, we will implement NSXMLParser's delegate methods to get each element:

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:
    (NSString *)qName attributes:(NSDictionary *)attributeDict
    NSString  *currentDescription = [NSString alloc];
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
    curDescription = string;
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    if([elementName isEqual: @"CelsiusToFahrenheitResult"]);
    textFieldResult.text = curDescription;


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


About the Author

You may also be interested in...


Comments and Discussions

Questiongot error Pin
Member 1163621629-Apr-15 0:27
memberMember 1163621629-Apr-15 0:27 
Questionfor xcode 5 and ios 7 Pin
Ishwar Bhandari1-May-14 1:09
memberIshwar Bhandari1-May-14 1:09 
QuestionUse of undeclared identifier connection Pin
Ishwar Bhandari30-Apr-14 23:58
memberIshwar Bhandari30-Apr-14 23:58 
GeneralMy vote of 1 Pin
Amogh Natu9-Apr-14 8:49
professionalAmogh Natu9-Apr-14 8:49 
QuestionDoubt Pin
Member 1061972623-Feb-14 18:35
memberMember 1061972623-Feb-14 18:35 
AnswerRe: Doubt Pin
dsp120-May-14 20:24
memberdsp120-May-14 20:24 
QuestionWrong answer Pin
Member 105731695-Feb-14 3:23
memberMember 105731695-Feb-14 3:23 
AnswerRe: Wrong answer Pin
Camilla.W.5-Feb-14 4:21
memberCamilla.W.5-Feb-14 4:21 
Questioncall soap request for different function Pin
Member 100396196-Jan-14 18:15
memberMember 100396196-Jan-14 18:15 
AnswerRe: call soap request for different function Pin
Fernando Jhon8-Jan-14 11:01
memberFernando Jhon8-Jan-14 11:01 
GeneralRe: call soap request for different function Pin
Member 100396199-Jan-14 18:59
memberMember 100396199-Jan-14 18:59 
QuestionServer did not respond Pin
chirag.khatsuriya27-Dec-13 1:00
memberchirag.khatsuriya27-Dec-13 1:00 
AnswerRe: Server did not respond Pin
Safak Tarazan3-Jan-14 9:51
professionalSafak Tarazan3-Jan-14 9:51 
GeneralRe: Server did not respond Pin
chirag.khatsuriya16-Jan-14 0:44
memberchirag.khatsuriya16-Jan-14 0:44 
QuestionIOS7 & Xcode 5 Pin
Member 1039519111-Nov-13 16:44
memberMember 1039519111-Nov-13 16:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171018.2 | Last Updated 28 Jul 2013
Article Copyright 2013 by Safak Tarazan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid