Sunday 12 August 2012

What You Should Know About MonoTouch

After talking to many people about MonoTouch and Mono for Android I’ve found there are a lot of misconceptions surrounding them. These tools have great potential which could be overlooked if they are not understood. This blog post provides background information you need to decide if you want to dig deeper into MonoTouch or Mono for Android.

What is MonoTouch?

MonoTouch is a software development kit for Mac OS X that enables using the C# programming language and common .NET libraries to create native applications for Apple iPhone, iPad and iPod Touch (iOS) devices. MonoTouch has access to all native iOS APIs and UI elements.
MonoTouch can deploy and run iOS applications on the iPhone simulator, as well as physical hardware. iOS applications created by MonoTouch can be distributed on Apple’s App Store.

What MonoTouch is Not

“MonoTouch brings the C# language and the core of .NET to the iPhone, but does nothing to provide a cross-platform UI experience or for that matter any sort of mobile device cross-platform APIs.” – Miguel de Icaza
MonoTouch and Mono for Android are not multi-platform mobile solutions though they have been used as a component to enable multiplatform solutions.

The Mono Open Source Project

Mono Open Source Project LogoMono is an open source implementation of Microsoft’s .NET Framework. It is based on the ECMA standards for C# and the Common Language Runtime. The Mono project is led by Miguel de Icaza (pronounced like “Meegell they eekaza”), a renowned open source programmer from Mexico. The name Mono comes from the spanish word for Monkey.
The Mono open source project was launched in July, 2001 at Ximian. Novell purchased Ximian in 2003. Mono 1.0 was released in June, 2004. Mono was initially focused on Linux desktop applications but has expanded to supporting a wide range of architectures and operating systems. MonoTouch 1.0 was released in September 2009 and Mono for Android in April 2011.
Attachmate acquired Novell in April 2011. In May 2011, the Mono development team was laid off. Miguel de Icaza created Xamarin to continue development of Mono.
Miguel announced at the MonoSpace conference in July 2011 that Xamarian would focus their efforts on MonoTouch and Mono for Android. Xamarian would lead the Mono open source project but much of the development would be accomplished by the Mono open source community.
I believe Xamarin is in a better position now than when they were as part of Novell. Xamarin can now focus on its flagship products, MonoTouch and Mono for Android.

MonoTouch Compilation

MonoTouch compiles C# code down to a native binary. As MonoTouch evolves it will become increasingly difficult to tell the difference between a binary compiled in Xcode vs MonoTouch.
Apple does not allow implemention of runtime engines on iOS. This means that an application cannot run from memory it has written to. MonoTouch does Ahead of Time (AOT) compilation of C# code. Microsoft has a similar technology called NGen.
Apple restricts others from creating runtime engines on iOS because they want the best quality user experience. Steve Jobs published a statement explaining this in April 2010. The contraints Apple published in April 2010 were targeted at Adobe Flash and were later relaxed to avoid alienating products like MonoTouch. Apple will not hinder MonoTouch because this technology fully enables all features of iOS. Joseph Hill, the COO of Xamarin, gave a great interview covering this topic.
Performance is equivalent between C# code compiled with MonoTouch and Objective C code compiled with Xcode. Performance of an app created in MonoTouch can degrade if frequent calls are made to a native iOS library such as within a loop. This is due to marshaling. MonoTouch adds approximately 4 mg to the file size of an iOS executable (according to Joseph Hill in September, 2011 ).
MonoTouch will only run from within the MonoDevelop IDE running on Mac OS (This is different from Mono for Android which runs on Mac or Windows and can be run from MonoDevelop or an add-in to Visual Studio 2010). Xcode is a prerequisite for MonoTouch though MonoTouch does not use Xcode for compilation. MonoTouch uses the following components of Xcode:
  • iOS simulator
  • Code signing tools
  • Interface Builder
  • Profiler

Interface Builder

Interface Builder is an Xcode tool that creates UI interfaces for iOS and Max OS X. Prior to Xcode 4 it was a separate executable. Starting with Xcode 4, Interface Builder is incorporated into Xcode. Interface Builder stores an interface as a .nib file (short for NeXT Interface Builder) which is a Mac OS X package. A .xib file is a newer XML formatted version of a .nib and is converted to a .nib file at compile time.
As of MonoDevelop 2.8, MonoTouch projects support using Xcode 4 to edit Interface Builder files. Prior to that only the standalone executable version of Interface Builder included with Xcode 3 could be used with MonoTouch.
To use Interface Builder within Xcode 4, MonoDevelop generates a temporary Xcode project containing stub Objective-C classes for the C# classes that are registered with Objective-C, which allows these classes to be accessed from Interface Builder in Xcode. Outlets and actions created in these stub classes using Interface Builder are synchronized back to MonoDevelop.

MonoTouch Limitations

Since MonoTouch cannot implement a runtime engine like the .NET CLR, it cannot implement dynamic features like Reflection. Incidentally GetType() is available in MonoTouch. This works because metadata is preserved during compilation.
I have encountered a few instances where a feature or method from .NET compiled but did not run. I expect these issues to be fewer as time goes on. An example was that the Linq related methodThenBy would compile but not run.

MonoTouch & Mono for Android Viability

The team at Xamarin are effective, quick and very motivated. They deploy updates regularly, sometimes many times in one week. When a minor iOS 4 update was released, the MonoTouch team at Novell released a corresponding update in a day or two. Xamarian appears to be even quicker. MonoTouch support for iOS 5 was released on the day that iOS 5 was released, October 12, 2011.
I understand the Mono community will finish implementing features from the next version of C# many months before Microsoft does (based on the spec). Then they test what Microsoft releases and they retrofit in the same bugs as found in Microsoft’s implementation to stay compatible.
Xamarin is on good terms with Microsoft since the MonoSpace conference I attended in Boston was held at the Boston Microsoft office. Xamarin’s products benefit Microsoft by promoting use of .NET and benefit Apple and Google by making it easier for a pool of .NET developers to target their platforms.
MonoTouch/Mono for Android are not faddish tools that are going to blow over or be crushed by Microsoft, Apple or Google. The Mono project has been around almost as long as .NET. Mono and related tools have a long history and a lot of momentum.
MonoTouch and Mono for Android are impressive but not too good to be true. These tools expose the complete native API to a development environment that uses C# language and supporting .NET libraries. They don’t promise creating an app by dragging and dropping a few shapes and clicking a button. You cannot share environment specific UI code between platforms and you have to understand the UI paradigm and API’s within the target environment. This is still a lot of work!

Why Use MonoTouch & Mono for Android

  • Your developers are more familiar with C#.net than Objective C (for iOS) or Java (for Android).
  • To allow sharing business logic between Windows, Windows Phone, ASP.net, iOS and Android platforms.
  • To enable a C#.NET developer to become productive more quickly on iOS or Android. Much of what they learn about the native API will be transferable to developing with that environment’s default toolset and language should they decide to switch later.
  • You view C# as a more productive language than Objective C.
  • To have a common programming language for mobile development across the leading platforms.

Why not call it MonoDroid?

Mono for Android was originally referred to as “MonoDroid” however, the word “droid” is a registered trademark of Lucasfilm Ltd. I assume Novell did not want to pay license fees for use of this name.

.NET Assemblies Available in MonoTouch

The following .NET assemblies are part of MonoTouch:
  • mscorlib.dll
  • System.dll
  • System.Core.dll
  • System.Json.dll
  • System.Runtime.Serialization.dll
  • System.ServiceModel.dll
  • System.ServiceModel.Web.dll
  • System.Web.Services.dll
  • System.Xml.dll
  • System.Xml.Linq.dll

MonoTouch Requirements

  • A Macintosh Computer running SnowLeopard or Lion
  • Xcode and the iOS SDK (you must be a registered Apple Developer to download Xcode)
  • Mono framework for Mac OS X – Intel Runtime
  • MonoDevelop IDE
  • MonoTouch

MonoTouch Cost

The Mono framework and MonoDevelop are open source and free. MonoTouch and Mono for Android extend the Mono framework to iOS and Android operating systems. These products are not free.
See MonoTouch and Mono for Android pricing here by selecting “Add to Cart” (you will not be obligated to buy anything unless you choose to).
*What are your impressions or experiences with MonoTouch or Mono for Android?