Getting Started with MonoTouch

Recently, I bought an iPad and decided to try my hand at objective-C. It was a pretty fun experience but as I began to think of apps that I’d actually want to write, I knew of a lot of open-source tools (either written by me or others) that I’d want to use. I definitely didn’t want to try re-writing them in objective-c. I had remembered a coworker of mine mentioning MonoTouch to me in the past as a C# compiler for the iPhone so I decided to check it out. Sure enough, they target both the iPhone and iPad. So I decided to download it and give it a try.

This guide is intended to help you get started with a project you can deploy to your device. If you want to simply develop with the simulator, not all of these steps will apply to you. You can simply scroll down to ‘Your first Monotouch application’ and read from there.

Before We Get Started

There has been a fair amount of concern and discussion regarding the apple developer agreement for iPhone OS 4.0. This had put me off for a while as I didn’t want to spend a lot of time working on an app that ultimately wouldn’t be approved. Having monitored the community for a few months, it appears that people are still getting their apps approved with regular frequency. This isn’t to say that Apple won’t one day decide to start rejecting apps — but it is a promising sign nonetheless. The people at MonoTouch certainly don’t think this is the end for their platform, but it’s up to you to decide if you want to take the risk. I plan to, not only because I like writing code in C#, but because I want to be able to take advantage of several .NET features supported by MonoTouch — WCF and Reflection, for example, and because I want to be able to use certain technologies like an IoC container.

Getting Started (what will I need?)

  • A mac
  • XCode
  • MonoDevelop (http://monodevelop.com/Download)
  • MonoTouch (http://monotouch.net/DownloadTrial)
  • An Apple Developer Key (http://developer.apple.com/iphone/index.action)

Once you’ve downloaded MonoDevelop and MonoTouch and installed them, it’s time to get your Apple Developer key. Follow the above link and begin enrollment — you’ll need to select “iPhone Developer Program” as your development program. You’ll also need to enter the pertinent required information (billing/contact information). Agree to their developer terms (“I agree to sell my soul to Apple…”) and head to their checkout process. The privilege of being an apple developer runs about $99 (plus tax), so be prepared to fork over some money if you want to do anything more than develop on a iPhone or iPad simulator, and if you ever want to put your app on the market. Once you’ve placed your order, wait for your e-mail to arrive. For me, this took a couple of hours. They say it can take up to 24, so try to be patient.

But to be honest, the process wasn’t painless for me. I got my activation code, tried to verify and got this dreadful message:

“We are unable to activate your Apple Developer Program membership.”

Unfortunately that set this post back a few days while I waited through the weekend to be able to call up their support number and get things settled. Luckily they said they could resolve it on their side and everything got worked out for me. So, now that I have my key, things can move forward!

Provisioning your apps

Before you can deploy with MonoTouch to your iPhone or iPad, you have to provision your environment for deployment. Apple’s process requires a lot of manual work, which I will walk you through below. They also include videos on their site to walk you through it. This will install the necessary keys on your machine that will be used to sign and deploy your code. To begin this process, you will need to go to the iphone developer center (developer.apple.com) and request a certificate. To do this, you first need to make a certificate request from the keychain on your mac.

How to create a certificate request

Go to Finder, then Applications, then Keychain Access. Once this comes up, we’ll need to check the preferences. In the menu, go to Keychain Access, and then Preferences. Click the Certificates tab and ensure that both Online Certificate Status Protocol and Certificate Revocation List are set to ‘off’. Once this is done, close the preferences window. Now, in the menu, go to Keychain Access, and then Certificate Assistant, and then Request a Certificate from a Certificate Authority. We will use this application to generate a certificate request to submit to Apple.

Put in the e-mail address that you used to sign up for the developer program, and put in your name. Select “save to disk” and “Let me specify key pair information”. Click continue and select a location to save the request to. In the next menu, ensure the key size is 2048 bits using the RSA algorithm. Click continue to finish the process.

Requesting a certificate from Apple

Go to the developer portal and select the ‘Certificates’ menu option on the left. You should see an empty list in the ‘Development Tab’ and a ‘Request Certificate’ button. Click the button, and upload the certificate request you generated before. You will be e-mailed a notification from Apple once you have done this. After a short amount of time, your request should be approved. Once it has been approved you should be able to download the certificate from apple. Once you have done this, open the certificate to have it automatically added to your keychain in the Keychain Access program.

Adding your device for deployment and debugging

After you have gotten your certificate approved and added to your keychain, we will need to set up your device for deployment. Attach your iPod, iPad or iPhone to your mac and open iTunes. In the ‘Devices’ section, select your device to view the summary for it. Click the ‘Serial Number’ label to reveal your identifier, which we will need to enter into the apple portal. Once you have revealed it, select the ‘copy’ option in the edit menu. Go to the developer portal and select the ‘Devices’ link in the left menu. Once the ‘Devices’ page has loaded, click the ‘Add Device’ button in the upper left. Enter a name for your device, and then paste the identifier we copied from iTunes. Click submit to add the device. 

After you have added your device, you will need to generate an app ID for your application. App IDs are used for a few different purposes (communicating with hardware, supporting in-app purchases, etc), and is a required part of your application setup. Click on the ‘App IDs’ link on the left menu, and then click the ‘New App ID’ button.  You will need to add a decription and then put in your app id suffix. Apple recommend you do a reverse domain setup. Fill in the required fields and click ‘submit’.

Once you have completed these steps, you’ll be ready to create a provisioning profile. Click the ‘provisioning’ menu option on the left menu and then ‘New Profile’ when the page loads. Enter a profile name and then select your certificate, appID and device and click submit. You will be taken back to the provisioning page where you can then download your profile. If the status says ‘in progress’, give it a minute and refresh the page. Download the profile to your mac.

 

Setting up MonoTouch and your Application for Deployment

Now that you have all the requisite parts, you are ready to set up MonoTouch to deploy to your device. First, you will need to install the provisioning profile you just created. Double-click the profile to open it in XCode. It should automatically be installed from there, and you can close XCode once that is complete. 

Your first MonoTouch application

Until this point we’ve focused on the logistics of getting everything set up. While this was not strictly necessary if you only wanted to do development with the simulator, it is necessary if you want to deploy your application to your device. Open MonoDevelop and start a new project. Expand the C# section, and select the iPhone and iPad node. Select either the iPhone or iPad window based project (I am using the iPad project). Name your application — I am naming mine Test Application — and click the ‘Ok’ button.

Once your project is set up, you can double-click the MainWindow.xib file to bring up the interface builder. I’m not going to go into too much detail here, but basically you will want to put something on the view to show that your application is starting and displaying correctly. I put a label on the page with “Hello World!” as the text. To run this in the simulator, go to the ‘Run’ menu item and select ‘Run’. It will compile and run the application in the simulator. Cool, huh?

Deploying your app to your device

This is all good and fine, but lets get it deployed on your device! Luckily, MonoTouch should automatically pick up the required information now that it is installed on your mac. But if it doesn’t, follow these steps:

Go to the ‘Project’ menu item, and select your projects options. For me, this is called Test Application Options. If you named your solution differently, it will be different. The options window will open, and under the ‘Build’ section, click the ‘iPhone Bundle Signing’ item. Select the identity you set up with apple from the ‘Identity’ drop down and then select the provisioning profile you set up with apple from the ‘Provisioning profile’ drop down and click the ‘OK’ button.

Finally, you will need to set the build configuration to debug|iPhone. This will direct MonoTouch to build and deploy your application. Go to the ‘Run’ menu option and select ‘Run’. You may get a message that says ‘codesign wants to sign using key (key name) in your keychain’ … select ‘Always Allow’. Once it completes, you will get a window that says the app has been built and deployed to your device. Close this window, and start your device. Scroll to the app and touch it. It should start, and once started, you should see a ‘Hello World!’ message.

Getting started with MonoTouch is very easy and I am very pleased with how well they integrate MonoDevelop with the interface builder, the device simulator, and the device deployment process. Setting up with Apple is a very tedious process, but I am glad to see they did not carry this over into MonoDevelop or MonoTouch. 

 

If you have any questions or problems, please leave a comment and I will do my best to help out.

Happy coding!

Related Articles:

    Post Footer automatically generated by Add Post Footer Plugin for wordpress.

    This entry was posted in MonoTouch. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
    • http://ltuasoo.wordpress.com/ Rennie

      Thanks for the article – I’m feeling inspired! :-)

      Does it have to be a Mac?

      If so, does it have to be a real Mac, or are there Mac virtual machines for PCs?

      Thanks again.

    • Joe

      Get out of here, PC!

    • http://www.lostechies.com/members/mbratton/default.aspx mbratton

      @Rennie – technically no, as long as it’s osx … I think if you can get a hackintosh running you should be good!

    • http://simpleprogrammer.com John Sonmez
    • http://awkwardcoder.com Byron Sommardahl

      This was SO helpful. Thanks for taking the time to do this. My app is now deployed to my iPhone and working like a champ!

    • MichaelW

      Rennie, MonoTouch is not supported on a hackintosh.

    • http://beyondfocus.com Joseph Hill

      We do not do any testing or take any effort to make MonoTouch work with Hackintosh images. We generally just ignore them, as they are in violation of Apple’s license. That said, from talking to users, it seems that while the MonoTouch eval works fine on the hackintoshes, the full version (which includes the full cross-compiler tool chain, copy protection mechanisms, etc) will not work on most hackintoshes for various reasons.

    • http://www.lostechies.com/members/mbratton/default.aspx mbratton

      @Joseph – Thanks for clearing that up!

    • Andy

      Are you saying in this article that the trial MonoTouch can be used to get apps that can be deployed to your own device? Or did you purchase the full MonoTouch?

    • http://www.lostechies.com/members/mbratton/default.aspx mbratton

      @Andy

      The full version is required. The trial version will allow you to work with the simulator only.

    • Amanda Adams

      Hi
      I do not have the Monotouch Activation Application on my MAC – PLease help me. I have an activation code, but no form to activate it with!!!!

    • http://www.lostechies.com/members/mbratton/default.aspx mbratton

      @Amanda

      Have you went to /Developer/MonoTouch/Applications/ and tried running the activation application from there? If that isn’t there, you might try redownloading/installing monotouch.

    • fds

      Hi, i program usually with monotouch in my hackintosh. i dont understand why a lot of people answer that things if dont know or try it after… :P

    • Sedot Sedot

      About IoC for monotouch, take a look on https://github.com/gserjo/TikoContainer

    • Pritsrk0786

      Hey I don’t want to put my App to App Store just to deploy it to iPad for that i need to perches the $99 & register to iOS developer program