MonoTouch ServiceStack

MonoTouch tutorial Step 3 - Calling the Web Service from MonoTouch

Setting up the RemoteInfoClient MonoTouch project

The Mono team has done a pretty good job keeping the full C# development experience without having too many limitations when developing with MonoTouch.

One such limitation is trying to reference Mono Develop projects that target the Framework 3.5. Which we would ideally like to do to access the RemoteInfo.ServiceModel project. Thankfully working around this is easy as you can just add a reference to the 'RemoteInfo.ServiceModel.dll' located in the 'bin/Debug' output folder directly. Other than that the only dependency you'll need is a reference to the 'ServiceStack.Client.dll' which is a light-weight assembly containing the ServiceClients required to access XML and JSON Web Services.

The MonoTouch example application provided is a standard MonoTouch application that just contains a navigationController and a TableViewController and is based on samples from the tutorials below:

With the required references set we can start. Even though there is no concept of an App.config file for MonoTouch projects, I still prefer to keep all my app's configuration within a single class. So in keeping the same spirit I've defined the Service Client for my app in AppConfig.cs.

The only custom class class used for this first example is the RemoteFilesTableViewController.cs custom TableViewController class:

Custom TableViewController

Which is effectively standard custom TableViewController class with just 2 properties: string CurrentPath { get; set; } to hold the CurrentPath that this TableView is displaying List<object> Items { get; set; } which holds the list of Items available at the CurrentPath.

The actual Web Service call is made in void ViewDidLoad () method which makes a request for the DirectoryInfo available at the CurrentPath. The results of the Web Service are then stored in the Items property.

Rendering the TableView is the responsibility of the nested TableView DataSource class below:

TableView DataSource

As it was based on the template from the tutorials above, it is a nested class named DataSource contained within the RemoteFilesTableViewController.

The logic for the DataSource class is pretty simple. It basically just iterates through each item and determines whether it's a File or a Directory. If it's a directory then we indicate to the user that they can navigate deeper by setting the cell to display a DisclosureIndicator. Directories are also prefixed with a '/' and their text is set to a Dark Green to provide more visual cues that they are different to a FileResult.

Handling the row selection is the responsibility of the nested TableView TableDelegate class below:

TableView Delegate

The TableDelegate like its DataSource sibling is also a nested class which allows you to provide custom handlers to handle TableViewController events.

Our implementation is quite simple we just Write to the Console the index of the row that was selected and if a directory was selected we determine its 'full path' and pass that into a new instance of RemoteFilesTableViewController which will be displayed on the screen.

So far that's all there is to it. To test it make sure you have XSP running, then select the 'Debug|iPhoneSimulator' configuration profile and set RemoteInfoClient as the 'Startup Project'. If all is well when you hit Debug you should have a running MonoTouch application that looks like the one below:

Now where getting somewhere, we now have a MonoTouch application calling XML Web Services! Since we're on a roll, lets see how easy it is to create a new Web Service.