Working with Web services in iPhone

Before you start reading in detail i would request you read this post of mine so that you will come to know in detail regarding the usage of a web service.

In this post we will learn how to use a webservice by sending a SOAP request to the server and reading the output which will be in xml, so in this post I have used the webservice that is provided by w3schools. 

The webservice provided by w3school is used to convert the Celsius temperature into Fahrenheit, the output returned by any webservice can be in json or xml and in this demo the webservice returns an xml structure that will be parsed by using an xml parser

Design Phase:  For the design phase we will make it simple not too much flashy, so here’s a view at the final output.



Design Explanation: The first text field will accept the user input for the temperature that he wants to convert into Fahrenheit and then will display the output in the other text field (Fahrenheit text field).

Step 1: Open Xcode and select the windows based application from the ios template, give this project an appropriate name and then add UIViewController subclass file to this project with the name myView, so now you must be having two new files added into your project that’s myView.h and myView.m, now you can either use interface builder or you can manually code for this view. Get the design ready in the first step just like the above pic and then go to step 2.

Step 2: For making a call to the webservice running in some server you need to call that service via SOAP request, so inorder to know whats the format of your soap request you need to go to the url where your web method is and their you will get the list of appropriate methods that you may want to use so select the appropriate method, below given are the snaps that will help you out for the web method that I am using 

Webservice main page



The SOAP request and response page



Step 3: To execute a web method you need to first need to create the soap body format and pass some of the parameters that are required by that method, and that you can do with the help of the below line


NSString *soapFormat = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<soap:Body>\n"
"<CelsiusToFahrenheit xmlns=\"http://tempuri.org/\">\n"
"<Celsius>%@</Celsius>\n"
"</CelsiusToFahrenheit>\n"
"</soap:Body>\n"
"</soap:Envelope>\n",txt1.text];

Next you need to specify the location of the web method and that you can do with the help of the NSURL class


NSURL *locationOfWebService = [NSURL URLWithString:@"http://www.w3schools.com/webservices/tempconvert.asmx"];



Now you need to create a soap header  by using the class NSMutableURLRequest , remember soap body and soap header are two different things, 


NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc]initWithURL:locationOfWebService];
NSString *msgLength = [NSString stringWithFormat:@"%d",[soapFormat length]];
[theRequest addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:@"http://tempuri.org/CelsiusToFahrenheit" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
//the below encoding is used to send data over the net
[theRequest setHTTPBody:[soapFormat dataUsingEncoding:NSUTF8StringEncoding]];


and finally you check whether the connection is established or not with the help of NSURLConnection as it provides support for loading the URL Requests


NSURLConnection *connect = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
if (connect) 
{

webData = [[NSMutableData alloc]init];
}
else {
NSLog(@"No Connection established");

}


In the above code I have took a member of NSMutableData and initialized it, this variable will have the entire xml structure that will be required by us to parse, 

The entire code looks like this


-(IBAction)invokeService
{
if ([txt1.text length]==0) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"WebService" message:@"Supply Data in text field" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"ok",nil];
[alert show];
[alert release];
}
else {
[txt1 resignFirstResponder];





NSString *soapFormat = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
"<soap:Body>\n"
"<CelsiusToFahrenheit xmlns=\"http://tempuri.org/\">\n"
"<Celsius>%@</Celsius>\n"
"</CelsiusToFahrenheit>\n"
"</soap:Body>\n"
"</soap:Envelope>\n",txt1.text];
NSURL *locationOfWebService = [NSURL URLWithString:@"http://www.w3schools.com/webservices/tempconvert.asmx"];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc]initWithURL:locationOfWebService];
NSString *msgLength = [NSString stringWithFormat:@"%d",[soapFormat length]];
[theRequest addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:@"http://tempuri.org/CelsiusToFahrenheit" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
//the below encoding is used to send data over the net
[theRequest setHTTPBody:[soapFormat dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *connect = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
if (connect) {
webData = [[NSMutableData alloc]init];
}
else {
NSLog(@"No Connection established");
}
}
}



Step 4: The NSURLConnection class has delegate method that we must implement so that we can read the xml structure that is returned by the webservice, if you want to see the xml format of the webservice that is returned then it will be provided just below the soap request section.

The delegate methods of NSURLConnection that you will be using are 

didReceiveResponse: In this method you set the mutabledata’s length to zero so that the data present from any previous request is clear

didReceiveData: Append the mutabledata variable with the data received from the webservice

didFailWithError: If the internet connection crashes then write code inside this method to prompt a message to the user for connection failure.

connectionDidFinishLoading: You will be writing code inside this method once the loading of the xml output is done in the mutable data

So heres how the entire code looks


//NSURLConnection delegate method

-(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");
[connection release];
[webData release];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received Bytes: %d", [webData length]);
xmlParser = [[NSXMLParser alloc]initWithData:webData];
[xmlParser setDelegate: self];
[xmlParser parse];
[connection release];
}



Step 5: Now its time to parse the xml for this I haven’t used GData Parser I have used the NSXMLParser to parse the xml, so from step 4 you can see I have allocated and initialized memory space for the parser and it has some delegate method that I have used to get the work done


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[nodeContent appendString:[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"CelsiusToFahrenheitResult"]) {
finaldata = nodeContent;
output.text = finaldata;
}
output.text = finaldata;
}


You may use any xml parser of your choice

Now go to the appDelegate.m file and add the view to your window


#import "webserviceDemoAppDelegate.h"
#import "myView.h"

@implementation webserviceDemoAppDelegate

@synthesize window;
#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.
    
myView *obj = [[myView alloc]initWithNibName:@"myView" bundle:nil];
[window addSubview:obj.view];
    [self.window makeKeyAndVisible];
    
    return YES;
}


press build and go to run the application, here's a view at the final output





You may download the source code from this link.

If you are god with dot net then you may create your own SOAP based services, just follow the given tutorial below:

- Creating SOAP based services part 1
- Creating SOAP based services part 2

iHope this post has helped you out for dealing with webservice,  any comments good or bad are always welcome until then Happy iCoding and have a great Day. You can also request me a tutorial

Follow iPhone by Radix group on facebook.

Comments

  1. Once I have Put up My all the efforts to Learn WebService searches 700 pages in a day , but I didn't get Such understandable code ...!

    Cheers ! Ravi SIr ...@

    ReplyDelete
  2. I think i know who this is
    Shreyas you could have asked me before searching 700 pages from google anyways nice to know that this article has helped you.

    ReplyDelete
  3. Hey, i just started coding in x code 4, can you please give the source code of this project, i want a thorough understanding of web service thing.
    Thanks in advance.

    ReplyDelete
  4. @harsh: Hi harsh hows it going hope you are doing fine. Please have a look at my facebook badge it contains my mail id, please send me a test mail to that ID so that i can forward you the coding for the same.

    And if you are a fresher then i would recommend that you go through the basics session of Objective C understand them and then come back to the webservice section that would be much better.

    ReplyDelete
  5. Thanks Radix, ya i know you are right, i ll start this thing from scratch once i get over with a important session tomorrow, i have to show a prototype.
    P.S. I m mailing you my ID on your gmail account.
    Thanks again.

    ReplyDelete
  6. I'm so happy that I found your blog

    This topic is very useful to me because I am a beginnings : >

    from here I start creating my Web service http://msdn.microsoft.com/en-us/library/8wbhsy70.aspx


    I was suffering (still) linking Web service with the My iPhone application

    Is it possible to give me source code :)

    ReplyDelete
  7. @Safia: hi how are how?

    After reading your comments i am a bit confused like you want me to post in a new tutorial on how to create a new webservice in asp.net or do you want me to give the source code for this application.

    If you want the source code then please send me a test mail on my mail ID the one which is given in the FB badge will mail u the source code.

    The job of the iPhone developers is to just make use of the webservices given to them into their project and if you have link to any such webservice that you are not able to crack then please mail me the link and if time permits then i will provide you the code for the same.

    ReplyDelete
  8. where is sourcecode ? thanks


    email:demirci.erhan@windowslive.com

    ReplyDelete
  9. Hi Man, nice blog. I am working to add/implement sharepoint for an Ipad application. So as to access List and everything. Something like "Moprise: Sharepoint Documents for ipad". Would really appreciate your Help! When I send a request using your code, the reply I get is the whole HTML Page.

    Regards,
    Urmil Setia

    ReplyDelete
  10. Hi man, Could you please send me the source code for above article.. my email id is amarjith.urs@gmail.com .thanking you

    ReplyDelete
  11. Hi sir,
    I am the new for the iphone/ipad, and i unable to understand the web service how to work in remote access and how the request data fetch from the server and display, could u send me the data flow of web service detailed process with a sample web service program..
    my mail id is: manimarans.365@gmail.com

    ReplyDelete
  12. Manimaran: Yes i shall prepare a ppt and mail you the flow and sorry for the delay in the reply.

    ReplyDelete
  13. Hi sir,
    I am the new for the iphone/ipad, and i unable to understand the web service .how we to get response and work with, how the request data fetch from the server and display.PLZ could u send me the data flow of web service detailed process with a sample web service program.also please send me all the basicinformation required for the same.
    my mail id is: ketandhoka16@gmail.com

    ReplyDelete
  14. what is nodeContent in parsing?

    ReplyDelete
  15. @anonymous: Node content is the content (Data) present in one node say for example
    Radix
    here Radix is the node content

    ReplyDelete
  16. Sir,could you please tell me how to parse multiple xml tags and display it in the text box or label or text view rather than in table view?Waiting for your reply..

    ReplyDelete
  17. Oops!!I forgot to add using "webservices"...

    ReplyDelete
  18. how do you do this using the latest Xcode where there is an error when you try to "release" anything? thank you for your help

    ReplyDelete
  19. Really a nice blog.. Thank you.. will follow this here afterwards

    ReplyDelete
  20. @Radix Its really a useful blog.
    Thank you for sharing it.

    ReplyDelete
  21. los puedes listar en una tabla y que sea dos campos porfavor? mi emal aldrin_ghost@hotmail.com

    ReplyDelete
    Replies
    1. the can list in a table and two fields it please? my emal aldrin_ghost@hotmail.com

      Delete
  22. thanks a lot for showing the demo of the webService

    ReplyDelete
  23. Excellent....

    Can you please tell me how to consume wcf service in ios.

    ReplyDelete
  24. Great information, I have try many others samples to understand how to use webservice on ios, this is the best one I saw. TYVM.

    ReplyDelete
  25. As I can send 2 data and return a boolean. How?

    ReplyDelete
  26. Hi sir,
    I am the new for the iphone/ipad devlopment, and i unable to understand the web service .how we to get response and work with, how the request data fetch from the server and display.PLZ could u send me the data flow of web service detailed process with a sample web service program.also please send me all the basicinformation required for the same.

    email ID:parthvaghasiya1991@gmail.com

    ReplyDelete
  27. hi sir
    can u explain with json
    am creating login page
    var json='{"userDetails":[{"username":"'+name+'" , "password":"'+password+'","apply_class_id":"'+apply_class_id+'"}],"wsfunction":"user_authentication"}';

    on server this attribute is present i want validation and move to next page

    ReplyDelete
  28. Hi sir,
    I am the new for the iphone/ipad, and i unable to understand the web service .how we to get response and work with, how the request data fetch from the server and display.PLZ could u send me the data flow of web service detailed process with a sample web service program.also please send me all the basicinformation required for the same.
    my mail id is:nagendranmca14@gmail.com

    ReplyDelete
  29. hi sir,
    i m new in iphone development.
    i want to know about webservies and some more thing can u mail me me this source code about webservices?
    thnaksss...
    my id is maulikpatel3110@gmail.com

    ReplyDelete
  30. Hi,

    Could you send me the source code for the webservice as well.
    brianx041@gmail.com

    Thanks

    Brian

    ReplyDelete

Post a Comment