Monday, August 27, 2012

Writing your own SOAP based web service for iPhone

Part 1: Many of the times I have seen that in companies it is expected that the ios developer should know how to develop the web services for their iphone application and iPhone developers after doing lots of hard work in iphone technology they get stuck with how to develop web services for their ios application so this tutorial is an effort took by me to make the iOS developer understand how to develop a simple web service in dot net.

Note: By default any web service that you create in dot net is a SOAP based web service whose response is in XML format, but JSON is considered more flexible as a response so I will teach you that as well the tool that I will be using to create the web service is visual studio 2010. For creating web services in dot net it is compulsory that you should know the base language C#.

Now your main task is to create a web service so for creating a web service you need to add the web service file here which is a very easy task.

So let’s get down for creating a simple SOAP based service.

Step 1: Visual studio is a tool from Microsoft which is used to create business class application using C# as the base language. You can launch visual studio by typing devenv command in run of by directly pressing the visual studio icon from your desktop.

After you click the visual studio(VS) icon you will see a screen in front of you from their you have to select the File menu and from File menu select the New - > Project given below is the snap shot that will guide you regarding the same.

After you select the new project you will be asked various options like for which environment you would like to create you new project is it for desktop, console or web from all the given options you have to select ASP.NET Web Application and don’t forget to mention your project name which is given at the bottom of the screen I have given the name DemoServices.

After filling in all the details click the OK button. 

And if you see a screen just like the one given below then congratulations you have successfully created a web app. 

Step 2: The best advantage of using visual studio tool is that it has each and everything structured with itself and there is a systematic way of handling files for various purposes.

For adding the web service file right click your project name in this case DemoServices there will be a menu in front of you from that menu select the Add option, the Add option will have several submenu items in it from those sub menu items select New Item

After you select New Item there will be a form in front of you asking what kind of file you want to add in your project. Select the web service file from the list which will be present somewhere at the bottom select it give the web service an appropriate name and save it. The extension of the web service created in dot net is asmx which means active server methods.

After giving a meaningful name to you webservice hit the Add button after you do this you will see a screen like the one given below

And if you are seeing this screen then congratulations you have learned how to add a web service file into dot net project. 

By default there is the HellowWorld web service in your project and when you run the application the output will be in XML format and not JSON so in the part 2 of this tutorial we will learn how to create some other functions which we can use as per our application needs and display the output in JSON format.

Dot net follows a code behind model approach hence when you add the asmx file you will be seeing one more file with the extension .cs and this is the file were all the coding will take place hence separating the code from design.

Step 3: Given below is the image which contains few basic details regarding the code snippet and some comparison with Objective C language so that iOS developers can understand.

As I have said earlier that by default if you run the web services then the output will be in xml format so to convert the output in json format you need to import two namespaces (just like framework in objective c) in C#, the name of those namespaces is given below

We have to keep a note of it that the web services will be called from a hand held device so it’s very important for the developer to know any kind of error messages or success messages from the server side and to do this I have my own procedure what I do is I create a response class with some properties and assign values to these properties and throw the appropriate response depending upon the code scenarios.

Step 4: We will be creating a response class so that whatever response is collected after the execution it is stored in the response class and is returned back in JSON format to us.
So to create the response class you need to add a .cs (.cs indicates that the given file contains some code in it with c sharp as the base language) class file in your project the same way you added the web service file.

Hit the Add button and the file will be added in your project.
It’s time to code for the response class I have just added three properties in the response class which are as follows:

-    ErrorCode: This property will be used to print exception messages.

-    SuccessFlag: This property will be used to print the success message which will be 0 for failure and 1 for success.

-    ResponseValue: This property will be used to print the result of calculator operations.

Given below is the snap shot in which I have displayed how what is the code to write properties in C# with code explanation

Step 5: Alright then that would be it for this session of the tutorial in the next session I will show how to use this response class. Also you can download the code till now from here.

This code is only workable in visual studio in the next tutorial we will discuss how to work with the response class. I have used visual studio 2010 for this demo.

Some bullet points on web services

- Web services are created only to communicate with the server.

-         - Web service can be made in C#, PHP and Java.
   -  While creating the web service the developer who is creating the service should keep a note that the required value in the web service is validated like email, user id or date etc.

-        NULL value should not be returned from web service.

-    Web services are nothing but functions just like the normal instance methods that we create while developing any application the only difference is that they are used as a media for communication between the web server and the hand held devices.

I hope that you have understood the basic concepts of creating SOAP based web services in Dot net and in case if you have not then feel free to contact me via mail or via comments.

Join us on facebook iPhone by radix Facebook group.

Until then Happy iCoding and have a great day. 


  1. Thanks sir ji ,the tutorials are really really helpful for a novice iPhone developer..

  2. In iOS 5 we got GLKit, which added a lot of higher-level functionality missing from OpenGL ES. It notably does not include object loading or scene graph management, two of the most fundamental needs of 3D programs.

    Iphone Developer

  3. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. cheap resume writing services