Web services allows to transfer the data between different platforms. There are scenarios where iOS application needs to send or receive data using an external web service. In this article we are going to learn how to use consume a .NET web service from within an iOS application.
NOTE: For this article we will be using MonoDevelop
framework to create our .NET project. You might notice that URLS for
invoking the web methods differ from when using Visual Studio vs
Downloading Libraries for iOS Application:
We are going to utilize a couple of third party libraries in our application. Take a look at the following list:
library is used for making HTTP requests using iOS framework. Copy all
the files in the classes folder and in the External folder to a new
folder in your iOS application.
2) JSON Framework
library is used to convert the string into the JSON object. Copy all
the files in the Classes folder to a new folder in your iOS application.
from the above two libraries we are also going to use a number of
built-in frameworks. Add the following frameworks to your project:
Creating a .NET Web Service:
will start by creating a very simple HelloWorld service. Add a new web
service file to your project and name is "NotificationService.asmx". The
"HelloWorld" web method is shown below:
Pretty simple right!
Creating an iOS Client:
iOS client will consist of a button which when clicked will make a
request to the HelloWorld web method of the NotificationService. The
button click event is shown below:
URL points to the "HelloWorld" web method which will be invoked when
the button is clicked. The requestFinished and requestFailed methods are
implemented below which are called depending on the result of
When you run the iOS application you will get the following in your console.
ensures that the web method "HelloWorld" was called from the iOS
application and it returned string "Hello World" to the client. In the
next section we are going to demonstrate how to call a SOAP web service
Invoking Web Service with Parameters:
parameter less web method is quite easy. In this section we are going
to call a web service which accepts multiple parameters. The web service
implementation is shown below:
above web method "Greet" takes two parameters namely deviceToken and
userName and returns a concatenation between the two parameters. In
order to call this web method from an iOS we will need to construct a
SOAP envelope. The implementation is shown below:
good way to find out what header values are required is to use browser
or a .NET client to invoke the service and then view the contents of the
request header. The result is shown below:
code above shows the response returned from the web method "Greet". As
you can see the response is quite big and hard to parse. You can specify
what kind of response you are interested in and the web service will
adhere to it. The following implementation asks for the JSON response,
although it returns XML response but the response is greatly reduced.
In the next section we are going to create a generic handler will will return pure JSON response.
Creating Generic Handler:
Our simple generic handler will return plain JSON. Here is the implementation of the generic handler.
The handler is available on HighOnCoding at the following URL:
The HighHandler.ashx returns the following result:
Since, it is returning JSON format we can easily consume it using iOS application as shown below:
above code will display "John Doe" in the console. In the next section
we will return a list of objects from the web service which will be
consumed by the iOS application.
Consuming List of JSON Objects:
this section we are going to return a list of objects from the web
service instead of a single object. The list of objects will be returned
in the JSON format. The ASHX HttpHandler is implemented below:
the above code we just created a list of zombie objects and then
class. If you visit the URL you will get the following result:
we need to consume this JSON string from inside our iOS application and
populate a UITableView will the JSON object. The following code
initiates a GET request which returns the JSON string representing
requestFinished is fired when the response is returned. If the response
code is 200 then we proceed to extract the JSON object from the JSON
string and add it to the NSMutableArray called "zombies".
The output is shown below:
If you like to add more content to each UITableCell then you can use the content view portion of the UITableCell.
this article we learned how to communicate with the SOAP web service as
well as how to consume JSON using JSON Framework library. We hope this
article helps you when you are creating interoperability apps between
iOS and the .NET framework.