1. Fundamentals
In this chapter, I will guide you through the installation of the development tools you will need for this book. Specifically, we will install Visual Studio for Mac as well as Xcode. The latter is the native toolset for Mac developers. It delivers IDE, SDKs, and also the device simulators we will use. If you have these tools already installed, you can skip to the subsequent section.
After ensuring that all tools are ready, we will create the first Xamarin.iOS app for the iPhone and iPad. This app, shown in Figure , displays various alerts and responds to user actions. I will also discuss the available project templates that are delivered by Visual Studio. The same templates are available in Xcode, so Xamarin.iOS and Visual Studio let you access iOS platformspecific programming interfaces in a way similar to native development tools but with the ease and smoothness provided by the C# programming language. In this chapter, I will also discuss the basic aspects of designing user interfaces in Visual Studio and show you how to associate event handlers with events fired by visual controls.
Note
In this chapter, I will not discuss Visual Studio for Mac in detail. I will only discuss the necessary elements of this IDE. You can find a comprehensive description of Visual Studio for Mac in the book Beginning Visual Studio for Mac. Build Cross-Platform Apps with Xamarin and .NET Core by Alessandro Del Sole.
Figure 1-1.
The Hello, World! app we will build in this chapter. The app is executed in the iPhone X simulator.
Setting Up the Development Environment
To install Visual Studio for Mac, youll need a Mac with macOS Sierra 10.12 or above. Here, Ill be using either a MacBook Pro or iMac with macOS Sierra 10.16. Once you know that you meet basic platform requirements, you can download the Visual Studio installer from the following website: ). Click the Open button to continue.
Figure 1-2.
An installer for Visual Studio for Mac
Figure 1-3.
A confirmation dialog
Visual Studio installer will now verify your system configuration (Figure . Its header tells you Thank you for downloading Visual Studio. In this dialog, you simply press the Continue button.
Figure 1-4.
Visual Studio installer is inspecting the operating system
At this point, the Visual Studio installer might prompt you to install Xcode (Figure ). This happens only if you do not have Xcode already installed. According to this dialog, you can install Xcode concurrently with the Visual Studio installation. Note that the Xcode installation is optional and depends on your current system configuration. I assume that you start with a clean install of macOS and therefore explicitly show how to install Xcode.
Figure 1-5.
A dialog prompting for Xcode installation
To install Xcode, you can press the Get Xcode button shown in Figure ). Alternatively, to install Xcode you can open the Mac App Store locally and then look up Xcode. Irrespective of which method you choose, Xcode and all related developer tools will be downloaded and installed in the background. So, you can now go back to Visual Studio installer.
Figure 1-6.
Xcode page in the Mac App Store
The Visual Studio installer will now let you choose which components to install (Figure ). To reduce installation size, I uncheck the Android + Xamarin.Forms entry and only install iOS- and macOS-related components. Then, after you click the Install button, the actual installation process begins.
Figure 1-7.
Choosing components to install
Visual Studio will now download and install the components. This will take a while, depending on network speed. You will be informed about each installation step and the overall progress, as shown in Figure . Also, as depicted in this figure, macOS may prompt you for the administrator password several times during installation. Once installation has finished, an appropriate dialog appears. Note that to build and run apps in the simulator, you will need to wait until Xcode installation has finished.
Figure 1-8.
Installing Visual Studio for Mac
Hello, World! App
After installing the development tools, we can start building the first app. To jumpstart the Xamarin.iOS development, I will tell you how to create the project using the Single View app template. Then, we will supplement the app with a single button. This button will react to taps such that the native alert will be displayed. Subsequently, we will add specific actions to this alert. Displaying alerts is a typical functionality of not only introductory apps, but also real apps, where it is used to collect user input or get confirmation for performing irreversible operations.
Creating the Project
To create the project, open Visual Studio for Mac. A welcome screen, depicted in Figure .
Figure 1-9.
A welcome screen for Visual Studio for Mac
Figure 1-10.
Project template selection
The New Project creator lets you choose a template for your project. To filter the list of templates to items directly related to iOS apps, you click App entry under the iOS tab. A list of available project templates will then appear on the right. This list is divided into two categories: General and Games. In this book, we will only use project templates from the General group. This category contains the following templates:
Single View App You use this template to create the app, which comprises a single view; i.e., an app without any navigation, like the app shown in Figure .