Getting Started Building iOS Applications with MonoTouch

I have been flirting with the idea of getting into mobile development in my spare time. I have went so far as to offer myself out as a developer to startups in Chicago to develop simple iOS applications in exchange for tool licenses. I am a firm believer in the idea that a craftsman buys his own tools. I am not buying them with my own money here, but I certainly am with my effort.

I also wanted to turn this experience into a few blog posts that might help someone else who has chosen to go down the same path that I have.

So what does a .NET developer need to get started writing an iOS application? There are three things to consider here. The first being picking up Objective-C as a language, second is learning the iOS API and third is relearning how to do stuff we already know like consuming a JSON service. Put another way, we need to learn a language, a platform and the idiomatic way of doing things in this particular development culture. That is a lot to bite off in a short period of time and still be productive and getting stuff done.

For this reason, I chose to develop my first few applications using Xamarin's MonoTouch. Taking this route allows me to leverage my current skills with C# and the .NET platform while learning the iOS API and still manage to be productive. I can noodle around with Objective-C at a later time.

The first thing you will notice about MonoTouch is the price, at $399 it is quite a leap of faith. But you can install and use the evaluation version to your hearts content. You only have to pony up money once you are ready to ship to the App Store.

To get up and running with MonoTouch, you will need to install the following things in this order:

  1. XCode 4 - If you don't mind paying $5, you can get this from the Apple App Store.
  2. MonoDevelop - This is a completely free IDE for the Mono framework.
  3. MonoTouch Evaulation
    You will also need a Mac. Yes sorry, thems the breaks.

To create an iOS application simply select File > New Project and dig down to the MonoTouch iPhone project template. There are also templates for iPad and Universal. Universal allows you to create a single application that will work on both the iPhone and the iPad. You can also create library assemblies just like you would expect in Visual Studio.

There is one catch though, MonoTouch project types are limited in the amount of the framework you will have access to work against and what 3rd party libraries you can reference. It is similar to working with Client Profile projects in Visual Studio. Everything you reference needs to target MonoTouch as well.

For instance, I have been working on an application that consumes a Xml-Rpc service.  There is already a fairly awesome OSS library from Cook Computing for all your Xml-Rpc needs. You cannot simply download the dll and reference it in your project. The dll needs to be compiled as a MonoTouch assembly. And all code needs to conform to the limited framework profile of MonoTouch. Cook Computing's library has both client and server components in one library. The server components depend on HttpResponse & HttpReqeust which are not available in a MonoTouch application.

I was able to solve this problem fairly easily by creating my own fork of the Cook Computing library and pulling in only the types needed for client communication. I even went so far as to publish this work on GitHub so others can simply use my fork and get back to making things awesome. Yay, OSS!

If you would like to look at a couple nontrivial applications written using MonoTouch to get an idea of where to start, the Washington State Department of Transportation has a great application published to the App Store now that is fully open source. I used this application as a guide post when creating my first MonoTouch app which you can find here.

This should be enough to get you on your way to writing your first application. I have some more tips around using OSS libraries and Unit Testing, but that will have to wait for another day in another blog post. Enjoy and happy non-conformist .NET application development day.

Follow me on Mastodon!