Xamarin is a wonderful technology that allows developers like you to use your C# knowledge to create cross-platform apps. You may use Xamarin to create iOS, Android and UWP apps. In this tutorial, we’ll talk about creating an iOS app using C# with Xamarin.
1. A Mac(iMac, Macbook/Pro/Air, Mac Mini) or a Mac virtual machine
2. Visual Studio for Mac(Get it here)
3. Xcode 8.1 and above(Recommended 8.1 at this point the article was posted)
Getting the tools ready
Let’s get them tools ready. You need a Mac running on 10.11 or later. Download Visual Studio for Mac. Download the latest Xcode from the app store. Once you have everything, we’re good to go.
Visual Studio for Mac New Project
Let’s start by creating a new project. For this purpose, we’ll choose Single View App under the category iOS as shown below. You may choose other project types as well. In Visual Studio for Mac, you can create iOS app, tvOS app, Mac app, Android app and of course .Net core apps.
Start A New Project
Click next and you’ll be presented in a configuration page to choose devices and target firmware. Give your app a name. Click Next to choose source control and project location. In the preview version of VS for Mac, only git is supported. Click on Create to start the project. When you’re done you should have something like this.
Xamarin for iOS
Click the Play button to debug. An iOS simulator will show as follow. When it loads, you’ll have an iOS app created on VS for Mac. Congratulations 😊
Debugging iOS App
Exploring the project structure
Let explore Main.storyboard. This is like your MainPage.xaml if you come from Windows development. If you have the same problem as mine (Xcode is too new), you can actually switch to use Xcode as your designer like how you would in Blend on Visual Studio(Windows). To fix storyboard not loading, go back to Xcode 8.1 and you’ll be good. To initiate downgrade, head to https://developer.apple.com/download/more/ and download Xcode 8.1.xip. Replace the Xcode in Applications with the extracted Xcode 8.1.
Designer Load Error
For the purpose of this tutorial, I switched back to Xcode 8.1. You should have something that looks like this.
Note the Generic section. This is where you choose Generic(as in for iPad and iPhone) or specific device
Adding C# codes
Let’s start by adding some buttons and labels. It’s at the controls section just right below controllers and objects. Like Visual Studio in Windows, you can drag and drop. To adjust the name and properties, check your bottom right. It looks and feel the same like how Visual Studio in Windows does. Let’s try changing the label and button.
Let’s try to run this to see how it works on simulator(or a more familiar term emulator).
Now this is the fun part. Let’s try to put in some code. For this, let’s try a pop up message. Like how you first started coding in Visual Studio in Windows, double click the button. It will bring up ViewController.cs. This is what MainPage.cs equivalent in Windows. Let’s try to put in this code:-
UIAlertView alert = new UIAlertView()
Title = "Hello",
Message = "C# in VS on Mac debugging iOS"
Debug it and you should have a screen like this.
Congratulations again! You have now used a C# to code an iOS app via Xamarin. There is of course more to explore. You can do complicated apps as well. And Visual Studio for Mac Preview is just the beginning.
For more example of codes, samples, visit the resource below:
If you would like to know what APIs are there to use, check here:
Final thoughts before ending this article. The little geek in me shouts excitement when Visual Studio for Mac came out and you can leverage on all your C# kungfu in developing cross platform apps. Oh, there is also the nuget manager like in Windows as well, check out the screenshot below. Good luck and have fun.
I’m one with the Xamarin, the Xamarin is one with me 😎