Brian Long Consultancy & Training Services
Ltd.
February 2012
Accompanying source files available through this
download link
Note: This tutorial was written for MonoTouch v5.x, as developed and sold by Xamarin. Various aspects make it inappropriate as a resource for earlier versions of MonoTouch, for which you can use this earlier tutorial
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 from Xamarin, you have all you need to develop and debug native applications for deployment to iPhone, iPad® and iPod Touch®. This in-depth 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.
As well as working with Microsoft’s .NET, you can also use C# to build applications for Xamarin’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.
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 Xamarin’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 when building in the Release configuration, 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 Apple’s Xcode).
Assuming you have an Apple Mac you will need to install several things to start developing iPhone applications with C#.
Xcode and the iPhone SDK (also known as the iOS SDK) are 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 Xcode download links 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 Xcode 4, which includes the Xcode development environment as well as the iOS SDK. If you are not a member of an Apple Developer program you can get Xcode 4 and the iOS 4.3 SDK (around 1.7 GB), however if you've paid to become a registered Apple Developer then you can get Xcode 4 along with the new iOS 5 SDK (around 3.5 GB).
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.
You can find Mono for OS X on the Mono downloads page - the latest stable version at the time of writing is 2.10.8. The installation is, as is to be expected on a Mac, trivial - download and mount the disk image (.dmg file) and double-click the enclosed package (.pkg file). Downloading the Intel Mac mono runtime will install the Mono runtime, libraries and C# compiler, amongst various other bits and pieces.
You can download and install MonoDevelop from the MonoDevelop downloads page (the stable release at the time of writing is 2.8.6.3). This is also straightforward - download and mount the disk image and then drag the enclosed application icon into wither your main Applications folder or into your user-specific Applications folder.
The next step is to get the MonoTouch SDK from the Xamarin 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.
If you are just testing things out for now, download and install the trial version which requires you to supply your email address.
If you purchased MonoTouch you will need to download the full version via the MonoTouch Activation page.
The installation follows a common pattern - download and moust the disk image (.dmg file) and then open the installation package (.pkg file).
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