Mono

Using C# to Develop for iPhone, iPod Touch and iPad

Brian Long Consultancy & Training Services Ltd.
March 2011

Accompanying source files available through this download link

Page selection: Previous  Next

Note: This tutorial was written for MonoTouch v3.0, as once sold by Novell. Since that time the team behind MonoTouch have left Novell and formed their own company, Xamarin and MonoTouch is up to v5.0 (at the time of editing this note). Additionally, Apple's Xcode development tool is now at v4.0 whereas the version illustrated in the text and screenshots here was v3.x. Consequently various details mentioned in this tutorial are inaccurate with regard to the current version, which clearly isn't too helpful. An updated version of this tutorial can be found here.

Executive Summary

The iPhone® is clearly a very successful smart-phone and the ability to develop applications for iPhones opens up a new sector for developers. Initially, iPhone application development was only within the reach of Objective-C programmers directly using Apple’s CocoaTouch framework, but this is no longer the case.

With C# and Mono® in conjunction with MonoTouch 3 from Novell®, you have all you need to develop and debug native applications for deployment to iPhone, iPad® and iPod Touch®. This lengthy article looks at the process of building iPhone applications with C#, exploring various techniques and common application features.

There is also a much shorter introductory article that gives a quick run through of developing C# applications using MonoTouch available here.

Contents

Introduction

Mono MonoDevelop

As well as working with Microsoft’s .NET, you can also use C# to build applications for Novell’s Mono platform, meaning .NET programming skills can progress your application code base from solely targeting Windows (other than when working with Silverlight®) to working against Linux on various hardware platforms and also Mac OS X. Mono includes and enables various toolkits and libraries to support the UIs and technologies available on these platforms.

When developing Mono projects you can work within the dedicated Mono development environment, MonoDevelop. This is a free and open source tool (originally based on the SharpDevelop open source editor) that runs both on Windows and on OS X on a Mac.

The traditional route to iPhone development involves using Apple development tools on a Mac. This means programming in Objective-C in Xcode® in combination with UI development in Interface Builder to build a native iOS application that can be tested in the iPhone simulator and then deployed to an iPhone for further testing, before optionally going to Apple’s AppStore.

Note: Where OS X is the operating system on a Mac, iOS is the operating system on an iPhone, iPod Touch and iPad. An iOS device could be an iPhone, an iPad or an iPod Touch. In this article the use of the term iPhone typically means any iOS device.

MonoTouch

To bypass the learning curve of Objective-C and retain your .NET programming skills, an alternative path involves building your application using a .NET language with Mono (still employing the Interface Builder UI step) and using Novell’s MonoTouch toolkit.

MonoTouch was originally launched in September 2009 and it offers several things to facilitate generating iPhone applications via the Mono platform:

Note: your application starts off as a managed Mono application, with the Mono runtime environment and all the trimmings. The 'smart linker' will get rid of a lot of redundant code, but it is fair to say your MonoTouch application will still be noticeably larger than an equivalent application written directly in Objective-C.

At one point Apple prohibited any application not built with Apple development tools (essentially anything other than Objective-C) but fortunately that is all in the past now and MonoTouch applications are welcomed onto the AppStore.

Note: Whilst MonoDevelop can run on Windows, Linux or OS X, MonoTouch requires you to be working on a Mac (as does the Interface Builder tool from Apple’s Xcode suite).

Getting started

Assuming you have an Apple Mac you will need to install several things to start developing iPhone applications with C#.

Installing iOS SDK

The iPhone SDK (also known as the iOS SDK) is free, but you are required to register yourself at the Apple iOS Dev Center first. Registering involves answering some questions on what markets and platforms you develop for and then clicking a link in a verification email you'll receive. You are then taken back to the iOS Dev Center and sent another email confirming your Apple ID.

The Dev Center is where you will find the iOS SDK as well as various reference materials and guides. Of course any programming documentation or sample code will be in Objective-C, but that needn’t be a complete stumbling block.

Download the iOS SDK, which is listed as Xcode and the iOS SDK combining two development kits: the Xcode development environment (which includes Interface Builder) as well as the iOS SDK (versions 3.2.5 and 4.2 respectively, at the time of writing). The fact that this is a combined download adds some unnecessary weight to the file if you already have Xcode installed, as it's a 3.5GB download.

Installing Xcode

Note: Whilst the development tools and SDK are free, in order to deploy to a device or to the AppStore you must be enrolled in Apple’s iPhone Developer Program, which costs $99 per year. It costs nothing to run your applications in the iPhone simulator though.

Installing Mono

You can find Mono for OS X on the Mono downloads page - the latest stable version at the time of writing is 2.10.1_3. The installation is, as is to be expected on a Mac, trivial. This will install the Mono runtime, libraries and C# compiler, amongst various other bits and pieces.

Installing Mono

Installing MonoDevelop

You can download and install MonoDevelop from the MonoDevelop downloads page (the version at the time of writing is 2.4.2). This is also straightforward.

Installing MonoTouch SDK

The next step is to get the MonoTouch SDK from the MonoTouch store. MonoTouch is a commercial product and you need to buy a license in order to deploy onto a device, however a trial version will let you run on the iPhone simulator. A single user license for the professional version currently costs $399 and includes all released updates for 1 year.

Download and install the trial version, which requires you to supply your email address.

Before Moving On

Now it’s time to launch MonoDevelop for the first time. If you come from a Windows programming background you might have trouble getting used to some of the keystrokes in the MonoDevelop text editor. If so you should use the MonoDevelop preferences dialog and remap some of the editor commands to keystrokes you find more natural. You'll quickly work out which keys do not operate in an expected manner.

Also, after a few hours use, sometimes the Solution window in the open source MonoDevelop environment becomes unresponsive or the editor may throw an exception causing Code Completion etc to stop working. If this happens just close MonoDevelop and restart it.

Now at last you’re set, so let’s get started!

Go back to the top of this page

Go back to start of this article

Previous page

Next page