Write Apple iPhone and iPad Applications in C#
There’s no doubt that the Apple iPhone and iPad represent elegant hardware design. The rapid ascent of the platform’s success and the vast array of applications available for the OS prove that consumers like what they see.
Unfortunately for .NET developers, programming using the iPhone SDK is nowhere near as effortless compared to programming for Windows Mobile devices running Microsoft .NET Compact Framework. Seeing this as an opportunity to help the hundreds of thousands of C# .NET developers, Novell has created MonoTouch, a specialized addition to the open-source Mono Project.
The Mono Project is a cross-platform, open-source .NET development framework that essentially allows .NET developers to write .NET applications for deployment on Windows, Linux, or Mac OS X. MonoTouch leverages this framework to bring C# syntax and compilation to the iPhone and iPad environments.
And remarkably, it works, although getting C# applications to run on the iPhone still takes a bit of effort to configure the development environment, learn how Apple’s Interface Builder is used to create and connect GUIs to code, and finally run compiled applications on actual iPhone hardware.
In addition to already possessing at least intermediate-level knowledge of the C# language and the .NET Framework 2.0 or higher, you must satisfy several other requirements before seeing the final output that MonoTouch produces. Because MonoTouch runs only on Mac OS X 10.5 or higher, an Intel-based Macintosh is needed.
MonoTouch also requires the iPhone SDK 3.0 or higher. If your goal is to write applications specifically for the iPad, the iPhone SDK 3.2 is required, and as of the publication of this review, this SDK runs only on Mac OS X Snow Leopard 10.6.2 or higher. The SDK includes Apple’s Xcode environment optimized for Mac OS X and iPhone development using Apple’s Objective-C language and Apple’s proprietary Cocoa framework.
Although it’s not essential, having a basic knowledge of Objective-C 2.0 and Cocoa will help you more efficiently navigate the iPhone programmatic capabilities. Also, to deploy native iPhone applications beyond the simulator, developers must subscribe to Apple’s iPhone Developer Program. Standard annual membership starts at $99, and an enterprise program that allows for internal distribution of iPhone applications for companies with 500 or more employees is available for $299 a year. Visit Apple's iPhone developer web page for more details.
Developers can proceed with the installation of Project Mono components, beginning with Novell’s MonoFramework version 2.6.1 or higher and its MonoDevelop IDE 2.2 or higher. Even without MonoTouch, the MonoFramework and MonoDevelop combination lets .NET developers write C# applications that compile to native Mac OS X applications. Installing MonoTouch is the final piece required to fulfill all the dependencies. Launching MonoDevelop, developers can choose to create a new solution and select an iPhone or, if the iPhone SDK 3.2 is installed, iPad application type, and start coding. Figure 1 shows project options.
The Coding Experience
Writing iPad, iPhone, and iPod Touch applications is different from the unified Microsoft Visual Studio (VS) GUI-building, code-constructing experience. Regardless of whether you’re developing in the Xcode IDE with Objective-C or the MonoDevelop IDE with C#, GUIs for these mobile platforms must be constructed and connected using Apple’s Interface Builder utility. Constructing GUI applications in this abstract way takes some acclimatization, especially the way that outlets (Apple jargon for a link from a UI element to the code it interacts with) have to be dragged and connected to their intended handler interfaces.
It would have been a truly amazing coup if MonoDevelop could have somehow incorporated GUI construction into a very Windows-centric design/code mixture. However, those familiar with Mac OS X development know just how challenging such a feature request would be to fulfill given the very different way Apple GUIs interact with the code that drives them.
Fortunately, the pure code aspect of writing program logic in C# is a more modern, even liberating, experience compared to Objective-C. Anyone who has written an Objective-C program knows the hassles of verbose code, explicit memory management (and the memory leaks that can ensue as a result of improper object de-allocation), pointers, and the need to learn the voluminous class attributes associated with foundation libraries. The Mono framework is nearly identical to the standard .NET framework in constructs and functionality, so those who can write C# code in their sleep will immediately benefit from the productivity boost MonoTouch can provide.
MonoTouch samples supplied by various contributors can be separately downloaded from the monotouch.net website and are highly recommended to help learn from since the Mono documentation is sparse, incomplete, and sometimes missing entirely. A Wiki documentation system on the monotouch.net website awaits documentation contributions by the MonoTouch creators and developer community, so these sample applications are paramount to learning how MonoTouch performs its magic.
Additionally, MonoDevelop does have its share of idiosyncrasies, one of which is programs can’t be compiled and executed directly on a device in a single step the way they can with Windows Mobile device development. Instead, applications must be separately compiled into native code, then uploaded to the device via a separate Run -> Upload to Device menu item.
Fortunately, the ability to live debug directly on the authorized target device exceeds even Apple’s own Xcode mechanisms in that debugging can occur un-tethered from a USB connection. That means debugging can occur over Wi-Fi and, even more interestingly, over the Internet. For example, a limited beta test population could be harnessed with a debugging library that could capture and broadcast metrics, facilitate bug reporting, and provide live app monitoring access, making the iPhone application debugging process using MonoTouch substantially more powerful than Apple’s own developer utilities.
Additionally, because the libraries are based on C# 3.0, technologies like LINQ are available to MonoTouch developers. Nothing else on the Apple platform comes close to LINQ in terms of its incredibly efficient and powerful data integration functionality. Consequently, MonoTouch is the perfect solution for forms-based, data-intensive iPhone applications.
MonoTouch is hosted within MonoDevelop, a very minimal coding environment compared to VS and even Xcode. While MonoDevelop encapsulates the basic necessities expected in any modern code editor (colored syntax, error highlighting, auto-complete, and code folding), it still appears minimal the way most free open-source programming tools usually do.
MonoDevelop’s source control management hosts only Subversion. Although developers could manage code in other systems outside of the IDE, lacking plug-in integration for other source control management systems like Perforce, Git, or even CVS is just one example of the rudimentary enhancements MonoDevelop currently provides. These minor annoyances are by no means showstoppers, but they do emphasize the maturation level and dedicated resources behind the Mono project.
Fortunately, the basic coding conveniences mentioned previously are all supported and do indeed make .NET developers feel more at home in an otherwise alien OS environment.
The Final Verdict
Compiling C# code into a native iPhone application for the first time is a rush. While I know enough Objective-C to write rudimentary applications, my Java and C# skills are considerably deeper due to the broader popularity and market of platforms that those languages target. Additionally, dealing with lame memory management and object counter issues in Objective-C is so 20th century. Taking a .NET library and seeing it being consumed on an iPhone in a native application approaches pure magic and swings the door wide open for any .NET developer with the right resources to build sophisticated applications in a very short time.
However, I would be remiss if I did not mention the current conundrum faced by Novell and other entities developing technologies like MonoTouch for the iPhone OS. When Apple announced its 4.0 SDK, a clause in the developer agreement essentially stated that applications would need to be written only with Apple-blessed languages.
This could effectively shut out companies like Novell that offer an alternative to Apple’s language platform of choice. Novell has stated it intends to work with Apple and seek ways to satisfy such constraints while continuing to offer its Objective-C alternative. However, until a definitive solution is reached, developers considering MonoTouch for commercial iPhone 4.0 application development need to be aware of this situation.
Nevertheless, for those focused on iPhone OS 3.2 or earlier development, MonoTouch represents a remarkable technological feat that makes developing iPhone and iPad-specific applications using C# syntax and familiar .NET frameworks easy, and yes, even fun.