Building iOS Apps with Xamarin and Visual Studio
When creating iOS apps, developers typically turn to the languages and IDE provided by Apple: Objective-C / Swift and Xcode. However, this isn’t the only option—you can create iOS apps using a variety of languages and frameworks.
One of the most popular options is Xamarin, a cross-platform framework that allows you to develop iOS, Android, OS X and Windows apps using C# and Visual Studio. The major benefit here is Xamarin can allow you to share code between your iOS and Android app.
Xamarin has a big advantage over other cross-platform frameworks: with Xamarin, your project compiles to native code, and can use native APIs under the hood. This means a well written Xamarin app should be indistinguishable from an app made with Xcode. For more details, check out this great Xamarin vs. Native App Development article.
Xamarin had a big disadvantage too in the past too: its price. Because of the steep licensing cost of $1,000 per platform per year, you’d have to give up your daily latte or frappuccino to even think about affording it … and programming without coffee can get dangerous. Because of this steep price, until recently Xamarin appealed mostly to enterprise projects with big budgets.
However, this recently changed when Microsoft purchased Xamarin and announced that it would be included in all new versions of Visual Studio, including the free Community Edition that’s available to individual developers and small organizations.
Free? Now that’s a price to celebrate!
Besides cost (or lack thereof), Xamarin’s other virtues include allowing programmers to:
- Leverage existing C# libraries and tools to create mobile apps.
- Reuse code between apps on different platforms.
- Share code between ASP.Net backends and customer-facing apps.
Xamarin also offers a choice of tools, depending on your needs. To maximize cross-platform code reuse, use Xamarin Forms. This works especially well for apps that don’t need platform-specific functionality or a particularly custom interface.
If your app does require platform-specific features or designs, use Xamarin.iOS, Xamarin.Android and other platform-specific modules to get direct interaction with native APIs and frameworks. These modules provide the flexibility to create very custom user interfaces, yet still allow sharing of common code across platforms.
In this tutorial, you’ll use Xamarin.iOS to create an iPhone app that displays a user’s photo library.
This tutorial doesn’t require any prior iOS or Xamarin development experience, but to get the most from it you’ll need a basic understanding of C#.
To develop an iOS app with Xamarin and Visual Studio, you’ll ideally need two machines:
- A Windows machine to run Visual Studio and write your project’s code.
- A Mac machine with Xcode installed to act as a build host. This doesn’t have to be a dedicated computer for building, but it must be network accessible during development and testing from your Windows computer.
It greatly helps if your machines are physically near each other, since when you build and run on Windows, the iOS Simulator will load on your Mac.
I can hear some of you saying, “What if I don’t have both machines?!”
For Mac-only users, Xamarin does provide an IDE for OS X, but in this tutorial we will be focusing on the shiny new Visual Studio support. So if you’d like to follow along, you can run Windows as a virtual machine on your Mac. Tools such as VMWare Fusion or the free, open-source VirtualBox make this an effective way to use a single computer.
If using Windows as a virtual machine, you’ll need to ensure that Windows has network access to your Mac. In general, if you can
ping your Mac’s IP address from inside Windows, you should be good to go.
- For Windows-only users, go buy a Mac right now. I’ll wait! :] If that’s not an option, hosted services such as MacinCloud or Macminicolo provide remote Mac access for building.
This tutorial assumes you’re using separate Mac and Windows computers, but don’t worry—the instructions are basically the same if you’re using Windows inside a virtual machine on your Mac.
Installing Xcode and Xamarin
If you don’t have it already, download and install Xcode on your Mac. This is just like installing any other app from the App Store, but since it’s several gigabytes of data, it may take a while.
After Xcode is installed, download Xamarin Studio onto your Mac. You’ll need to provide your email, but the download is otherwise free. Optional: do a happy dance for all the coffees you can still afford.
Once the download is complete, open the installer package and double click Install Xamarin.app. Accept the terms and conditions and continue.
The installer will search for already-installed tools and check for current platform versions. It will then show you a list of development environments. Make sure Xamarin.iOS is checked, then click Continue.
Next you’ll see a confirmation list summarizing the items to be installed. Click Continue to proceed. You will be given a summary and an option to launch Xamarin Studio. Instead, click Quit to complete the installation.
Installing Visual Studio and Xamarin
For this tutorial you can use any version of Visual Studio, including the free Community Edition. Some features are absent in the Community Edition, but nothing that will prevent you from developing complex apps.
Your Windows computer should meet the Visual Studio minimum system requirements. For a smooth development experience, you’ll want at least 3 GB of RAM.
If you don’t already have Visual Studio installed, download the Community Edition installer by clicking the green Download Community 2015 button on the Community Edition web site.
Run the installer to begin the installation process, and choose the Custom installation option. In the features list, expand Cross Platform Mobile Development, and select C#/.NET (Xamarin v4.0.3) (where v4.0.3 is the current version when this tutorial was written, but will likely be different in the future).
Click Next and wait for the installation to complete. This will likely take a while; go take a walk to burn off all the cookies you ate while installing Xcode. :]
If you already have Visual Studio installed but don’t have the Xamarin tools, go to Programs and Features on your Windows computer and find Visual Studio 2015. Select it, click Change to access its setup, then select Modify.
You’ll find Xamarin under Cross Platform Mobile Development as C#/.NET (Xamarin v4.0.3). Select it and click Update to install.
Whew—that’s a lot of installations, but now you’ve got everything you need!