Building iOS apps with C#

Learning Java in the context of the Android platform has been, and still is, an interesting ride but it’s also a time consuming one, and when you’re having to jump between platforms and languages it can often leave you a little confused as you sit and stare at the compiler’s helpful little underline squiggle telling you that what you’ve just written is nonsense even though you’re sure it’s right, until you realise you’re still in Java mode when you’re using Visual Studio and C#! Doh! :)

So the idea of learning yet another language, in this case Objective-C for iOS apps, worries me a little bit. Will my tiny brain be able to cope with all this information? On the one hand, it’s a new skill, what’s not to like? On the other, it’s yet more time I need to find in order to get proficient at it, and unlike Java, Objective-C is not that similar to C# so the mental leap is a little bit bigger. There is another way though that we can leverage our existing skill set and it’s called MonoTouch which together with the MonoDevelop IDE gives us a complete solution to writing applications for the iPhone and iPad using C# and after having played with it for a few days now I have nothing but praise for it. I was amazed at how quickly I managed to get a basic application running. I even imported some C# classes written using Visual Studio which compiled with no changes at all.

Let’s compare a snippet of Objective-C with C# to create and show an ActionSheet which is a kind of dialog that slides up from the bottom of the screen.

First the Objective-C version:


UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"hello world"
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                           destructiveButtonTitle:nil
					                            otherButtonTitles:nil];
[actionSheet showInView:[[self view] window]];

Now the C# version:


var actionSheet = new UIActionSheet("hello world", null, "OK", null, null);
actionSheet.ShowInView(this.View.Window):

Now its not a complex piece of code by any stretch, and maybe I’m biased but I definitely think the C# code is easier to read, write, and understand (although I do concede that specifying the parameter names inline, as the Objective-C version does, aids the reader) so given the choice it’s obvious which one I would prefer to write in. And from a business point of view if you’re a .Net development shop and your customers are now asking for iPhone applications then making the most of your in-house skills should definitely be factored into the decision process. When you consider that all the things you know and love about C# are still available to you such as LINQ, lambdas, events, and even the ability to utilise dual cores through the AsParallel method on collections, as a .Net developer you have to have a real damned good reason to go off and learn Objective-C because it’s going to be a good while until you’re as proficient in that language as the one you’re already used to. Having said that, as the examples show, you still use classes that are part of the iOS SDK so maybe the language should more accurately be called Objective-csharp instead! :)

Are there any drawbacks? Well, aside from still needing a Mac, from a single developer point of view the main negative is the cost. At 399 euros its not an impulse decision but for a business it is insignificant compared to the productivity gained from a language and IDE familiar to your development team. The one other possible negative is that Objective-C coders might give you a hard time for not doing “real” iOS development but that’d be their problem not yours. The good news is that the trial never expires so you can keep developing in it and writing apps for iOS for as long as you like but you can only ever run them on the simulator. Obviously, if you write something worth releasing then you’ll need to take the plunge but as it stands its a great way to get into iOS development without throwing away your existing skill set.

I’ll write up my experiences with MonoTouch as I learn more over time but right now I’m really impressed by it and can’t wait to dig deeper.

Advertisements
Building iOS apps with C#