Xamarin Cross-Platform Application Development - Second Edition - Sample Chapter
Xamarin Cross-Platform Application Development - Second Edition - Sample Chapter
ee
Sa
pl
Xamarin Cross-platform
Application Development
Second Edition
Xamarin has built three core products for developing iOS and Android applications
in C#: Xamarin Studio, Xamarin.iOS, and Xamarin.Android. Xamarin gives you direct
access to the native APIs on each platform and the flexibility to share C# code between
platforms. Using Xamarin and C#, you get better productivity when compared to Java
or Objective-C, and still retain great performance compared to an HTML or a
JavaScript solution.
In this book, we will develop a real-world sample application to demonstrate what
you can do with Xamarin technologies, and build on core platform concepts for iOS
and Android. We will also cover advanced topics such as push notifications, retrieving
contacts, using the camera, and GPS location. With Xamarin 3, a new framework was
introduced called Xamarin.Forms. We will cover the basics of Xamarin.Forms and
how you can apply it to cross-platform development. Finally, we will walk through
what it takes to submit your application to the Apple App Store and Google Play.
Chapter 7, Deploying and Testing on Devices, walks you through the painful
process of deploying your first application to a device. We also cover why it
is important to always test your application on real devices.
Chapter 8, Web Services with Push Notifications, explains the technique
of implementing a real backend web service for XamChat using Azure
Mobile Services.
Chapter 9, Third-party Libraries, covers the various options of using
third-party libraries with Xamarin and how you can even leverage native
Java and Objective-C libraries.
Chapter 10, Contacts, Camera, and Location, introduces the library,
Xamarin.Mobile, as a cross-platform way to access users' contacts, camera,
and GPS location.
Chapter 11, Xamarin.Forms, discovers Xamarin's latest framework,
Xamarin.Forms, and how you can leverage it to build cross-platform applications.
Chapter 12, App Store Submission, explains the process of submitting your
app to the Apple App Store and Google Play.
Setting Up Xamarin
Xamarin's development tools have given us the power to develop native
iOS, Android, and Mac applications in C#, which is one of the most popular
programming languages. There are many advantages of choosing Xamarin to
develop mobile applications instead of Java and Objective-C. You can share code
between both the platforms and can be more productive by taking advantage of the
advanced language features of C# and the .NET base class libraries. Alternatively,
you would have to write the app twice for Android and iOS and lose the benefits
of garbage collection when using Objective-C.
In comparison to other techniques of developing cross-platform applications with
JavaScript and HTML, Xamarin also has some distinct advantages. C# is generally
more performant than JavaScript, and Xamarin gives developers direct access to the
native APIs on each platform. This allows Xamarin applications to have a native
look and perform in a manner similar to their Java or Objective-C counterparts.
Xamarin's tooling works by compiling your C# into a native ARM executable that
can be packaged as an iOS or Android application. It bundles a stripped-down
version of the Mono runtime with your application that only includes the features
of the base class libraries your app uses.
Setting Up Xamarin
In this chapter, we'll set up everything you need to get started on developing
with Xamarin. By the end of this chapter, we'll have all the proper SDKs and tools
installed and all the developer accounts needed for app store submission.
In this chapter, we will cover:
[8]
Chapter 1
Xcode: This is the core IDE for developing iOS and Mac applications
in Objective-C
Xcode Command Line Tools: These are installed inside Xcode, and
provide common command-line tools and scripting languages that
developers will find useful, such as Subversion, Git, Perl, and Ruby
The Mono runtime for Mac: This is required for compiling and running
C# programs on OS X
The Mono runtime for Mac: This is required for compiling and running
C# programs on OS X
Each of these will take some time to download and install. If you can access a fast
Internet connection, it will help speed up the installation and setup process. With
everything ready to go, let's move ahead step-by-step, and hopefully, we can skip
a few dead-ends you might otherwise run into.
It is important to note that Xamarin can also be used on Windows and
Visual Studio, even though it is not covered in this book. A Mac is
required for iOS development, so Windows developers must connect
Visual Studio to a Mac to compile for iOS. Luckily, most of what we
learn in this book can be directly applied to using Xamarin on Windows.
[9]
Setting Up Xamarin
Installing Xcode
To make things progress more smoothly, let's start off by installing Xcode for Mac.
Along with Apple's IDE, it will also install the most commonly used developer tools
on the Mac. Make sure you have at least OS X 10.8 (Mountain Lion), and locate
Xcode in the App Store, as shown in the following screenshot:
This will take quite some time to download and install. I'd recommend that you take
the time to enjoy a nice cup of coffee or work on another project to pass the time.
When that is out of the way, launch Xcode for the first time and progress through the
initial startup dialog. Next, navigate to Xcode | Preferences to open Xcode's main
settings dialog.
In the Downloads tab, you'll notice several additional packages you can install inside
Xcode. Here, you can download the official iOS documentation, which the Xamarin
installer will make use of. Optionally, you can install older iOS simulators, but we
can just use the default one for the content in this book. When you're finished,
your Xcode's Components section should look something similar to the
following screenshot:
[ 10 ]
Chapter 1
Installing Xcode installs the iOS SDK, which is a requirement for iOS development
in general. As a restriction from Apple, the iOS SDK can only run on a Mac. Xamarin
has done everything possible to make sure they follow Apple's guidelines for iOS,
such as restricting dynamic code generation. Xamarin's tools also leverage features
of Xcode wherever possible to avoid reinventing the wheel.
Installing Xamarin
After installing Xcode, there are several other dependencies that need to be installed
in order prior to developing with Xamarin's tools. Luckily, Xamarin has improved
the experience by creating a neat all-in-one installer.
Install the free Xamarin Starter Edition by performing the following steps:
1. Go to http://Xamarin.com and click on the large Download now button.
2. Fill out some basic information about yourself.
3. Download the XamarinInstaller.dmg file and mount the disk image.
4. Launch Install Xamarin.app and accept any OS X security warnings
that appear.
[ 11 ]
Setting Up Xamarin
5. Progress through the installer; the default options will work fine. You can
optionally install Xamarin.Mac, but this topic is not covered in this book.
The Xamarin installer will download and install prerequisites such as the Mono
runtime, Java, the Android SDK (including the Android emulator and tools), and
everything else you need to get up and running.
You will end up with something similar to what is shown in the following screenshot,
and we can move on to conquer bigger topics in cross-platform development:
Chapter 1
Indie Edition: This is available to individuals only, and it does not include
Visual Studio support.
Business Edition: This is available for companies; it adds features for Visual
Studio and includes better Xamarin product support.
[ 13 ]
Setting Up Xamarin
13. As Target, make sure that you select Intel x86 Atom System Image.
14. After creating the device, go ahead and click on Start to make sure the
emulator runs properly.
The emulator will take some time to start up, so it is a good idea to leave the emulator
running while performing Android development. Xamarin is using the standard
Android tools here, so you would have the same issue while developing with Java.
If everything starts properly, you will see an Android boot screen followed by a
virtual Android device ready for deploying applications from Xamarin Studio,
as shown in the following screenshot:
[ 14 ]
Chapter 1
[ 15 ]
Setting Up Xamarin
You should receive an e-mail that looks something similar to the following
screenshot within two business days:
[ 16 ]
Chapter 1
When all is said and done, your Contracts, Tax, and Banking section should look
something similar to the following screenshot:
With your iOS developer account successfully registered, you will now be able to
deploy to iOS devices and publish your apps to the Apple App Store.
[ 17 ]
Setting Up Xamarin
If you plan on selling paid apps or in-app purchases, at this point, I would recommend
that you set up your Google merchant account. This will enable Google to pay you the
proceeds toward your app sales by applying the appropriate tax laws in your country.
If you are setting this up for your company, I would recommend that you get the
assistance of your company's accountant or bookkeeper.
[ 18 ]
Chapter 1
[ 19 ]
Setting Up Xamarin
At this point, one would think that our account would be fully set up, but there
is one more crucial step prior to being able to sell apps: we have to enter the
banking information.
Setting up banking for your Google merchant account can be performed with the
following steps:
1. Go back to the Google Play Developer Console at https://play.google.
com/apps/publish.
2. Click on the Financial Reports section.
3. Click on the small link titled Visit your merchant account for details.
4. You should see a warning indicating that you do not have a bank account
set up. Click on the Specify a Bank Account link to get started.
5. Enter your banking information. Again, a company accountant might
be needed.
6. In a few days, look for a small deposit in your account from Google.
7. Confirm the amount by going to http://checkout.google.com/sell.
8. Click on the Settings tab, then Financials.
9. Next, click on Verify Account.
10. Enter the amount that appeared on your bank account and click on
Verify deposit.
Your Google merchant account is also the place where you can cancel or refund
customer orders. Google Play is different from the iOS App Store in that all
customer issues are directed to the developers.
Summary
In this chapter, we discussed Xamarin's core products for developing Android and
iOS applications in C#: Xamarin Studio, Xamarin.iOS, and Xamarin.Android. We
installed Xcode and then ran the Xamarin all-in-one installer, which installs Java,
the Android SDK, Xamarin Studio, Xamarin.iOS, and Xamarin.Android. We set
up the x86 Android emulator for a faster, more fluid experience when debugging
applications. Finally, we set up iOS and Google Play developer accounts for
distributing our applications.
In this chapter, you should have acquired everything you need to get started on
building cross-platform applications with Xamarin. Your development computer
should be ready to go and you should have all the native SDKs installed and ready
for creating the next great app to take the world by storm.
[ 20 ]
Chapter 1
The concepts in this chapter will set us up for more advanced topics that will require
the proper software installed as well as developer accounts with Apple and Google.
We will be deploying applications to real devices and implementing more advanced
features such as push notifications. In the next chapter, we'll create our first iOS and
Android application and cover the basics of each platform.
[ 21 ]
www.PacktPub.com
Stay Connected: