Beginning Xamarin Development for the Mac: Create iOS, watchOS, and Apple tvOS apps with Xamarin.iOS and Visual Studio for Mac 1st Edition Dawid Borycki All Chapters Instant Download
Beginning Xamarin Development for the Mac: Create iOS, watchOS, and Apple tvOS apps with Xamarin.iOS and Visual Studio for Mac 1st Edition Dawid Borycki All Chapters Instant Download
com
OR CLICK BUTTON
DOWNLOAD NOW
https://textbookfull.com/product/beginning-visual-studio-for-mac-
build-cross-platform-apps-with-xamarin-and-net-core-alessandro-del-
sole/
textboxfull.com
https://textbookfull.com/product/biota-grow-2c-gather-2c-cook-loucas/
textboxfull.com
https://textbookfull.com/product/beginning-ios-ar-game-development-
developing-augmented-reality-apps-with-unity-and-c-1st-edition-allan-
fowler/
textboxfull.com
Visual Studio Extensibility Development: Extending Visual
Studio IDE for Productivity, Quality, Tooling, and
Analysis Rishabh Verma
https://textbookfull.com/product/visual-studio-extensibility-
development-extending-visual-studio-ide-for-productivity-quality-
tooling-and-analysis-rishabh-verma/
textboxfull.com
https://textbookfull.com/product/beginning-ubuntu-for-windows-and-mac-
users-start-your-journey-into-free-and-open-source-software-haines/
textboxfull.com
https://textbookfull.com/product/beginning-ubuntu-for-windows-and-mac-
users-start-your-journey-into-free-and-open-source-software-nathan-
haines/
textboxfull.com
Dawid Borycki
Beginning Xamarin Development for the Mac
Dawid Borycki
Institute of Physical Chemistry,
Polish Academy of Sciences,
Kasprzaka 44/52, Warsaw, 01-224, Poland
ISBN-13 (pbk): 978-1-4842-3131-9 ISBN-13 (electronic): 978-1-4842-3132-6
https://doi.org/10.1007/978-1-4842-3132-6
Library of Congress Control Number: 2017963095
Copyright © 2018 by Dawid Borycki
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are
not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Cover image designed by Freepik
Managing Director: Welmoed Spahr
Editorial Director: Todd Green
Acquisitions Editor: Joan Murray
Development Editor: Laura Berendson
Technical Reviewer: Chaim Krause
Coordinating Editor: Jill Balzano
Copy Editor: April Rondeau
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Distributed to the book trade worldwide by Springer Science+Business Media New York,
233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505,
e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California
LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc).
SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected], or visit http://www.apress.com/
rights-permissions.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions
and licenses are also available for most titles. For more information, reference our Print and eBook Bulk
Sales web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub via the book’s product page, located at www.apress.com/9781484231319. For more
detailed information, please visit http://www.apress.com/source-code.
Printed on acid-free paper
This book is dedicated to my wife, Agnieszka, daughter, Susanna, and son, Xavier, with love.
We all have dreams. But in order to make dreams come into reality, it takes an awful
lot of determination, dedication, self-discipline, and effort.
—Jesse Owens
Contents
■
■Chapter 1: Fundamentals�������������������������������������������������������������������������������������� 1
Setting Up the Development Environment����������������������������������������������������������������������� 2
Hello, World! App�������������������������������������������������������������������������������������������������������������� 8
Creating the Project�������������������������������������������������������������������������������������������������������������������������������� 8
Storyboard Designer����������������������������������������������������������������������������������������������������������������������������� 16
User Interface��������������������������������������������������������������������������������������������������������������������������������������� 17
AlertViewController������������������������������������������������������������������������������������������������������������������������������� 20
Actions�������������������������������������������������������������������������������������������������������������������������������������������������� 23
Action Sheet����������������������������������������������������������������������������������������������������������������������������������������� 25
Summary ����������������������������������������������������������������������������������������������������������������������� 26
■
■Chapter 2: App Structure and Lifecycle��������������������������������������������������������������� 27
An Entry Point����������������������������������������������������������������������������������������������������������������� 27
AppDelegate������������������������������������������������������������������������������������������������������������������� 29
View Lifecycle���������������������������������������������������������������������������������������������������������������� 32
Information Property List������������������������������������������������������������������������������������������������ 34
Entitlements Property List���������������������������������������������������������������������������������������������� 35
Launch Storyboard �������������������������������������������������������������������������������������������������������� 36
Storyboards Under the Hood������������������������������������������������������������������������������������������ 38
Model View Controller���������������������������������������������������������������������������������������������������� 41
v
■ Contents
Persisting Data��������������������������������������������������������������������������������������������������������������� 42
Summary������������������������������������������������������������������������������������������������������������������������ 46
■
■Chapter 3: Views������������������������������������������������������������������������������������������������� 47
Basic Controls���������������������������������������������������������������������������������������������������������������� 47
Tables����������������������������������������������������������������������������������������������������������������������������� 50
Displaying Items����������������������������������������������������������������������������������������������������������������������������������� 51
Selecting Items������������������������������������������������������������������������������������������������������������������������������������� 55
Deleting Items�������������������������������������������������������������������������������������������������������������������������������������� 57
Web View������������������������������������������������������������������������������������������������������������������������ 61
Google Geocoding API��������������������������������������������������������������������������������������������������������������������������� 64
Invoking JavaScript Functions�������������������������������������������������������������������������������������������������������������� 66
Summary���������������������������������������������������������������������������������������������������������������������� 100
■
■Chapter 5: Touch������������������������������������������������������������������������������������������������ 101
Touches and Gesture Recognizers������������������������������������������������������������������������������� 101
Swipe and Long-Press Gesture Recognizers��������������������������������������������������������������� 103
Manipulating Controls with Gestures��������������������������������������������������������������������������� 107
vi
■ Contents
Summary���������������������������������������������������������������������������������������������������������������������� 116
■
■Chapter 6: Unit Testing�������������������������������������������������������������������������������������� 117
Creating a Model to Test����������������������������������������������������������������������������������������������� 119
Implementing Unit Tests����������������������������������������������������������������������������������������������� 120
Running Unit Tests�������������������������������������������������������������������������������������������������������� 126
User Interface Tests������������������������������������������������������������������������������������������������������ 127
Creating an App���������������������������������������������������������������������������������������������������������������������������������� 128
Xamarin Test Cloud Agent������������������������������������������������������������������������������������������������������������������� 128
Creating UI Tests��������������������������������������������������������������������������������������������������������������������������������� 130
Summary���������������������������������������������������������������������������������������������������������������������� 142
■
■Chapter 7: Consuming RESTful Web Services���������������������������������������������������� 143
REST Service Client������������������������������������������������������������������������������������������������������ 145
Updating Data������������������������������������������������������������������������������������������������������������������������������������� 148
Getting a Specific User����������������������������������������������������������������������������������������������������������������������� 149
Testing the REST Client������������������������������������������������������������������������������������������������ 149
Users Repository���������������������������������������������������������������������������������������������������������� 152
Presenting a List of Users�������������������������������������������������������������������������������������������� 155
Displaying User Details������������������������������������������������������������������������������������������������ 158
Summary���������������������������������������������������������������������������������������������������������������������� 162
■
■Chapter 8: watchOS������������������������������������������������������������������������������������������� 163
Creating the Project������������������������������������������������������������������������������������������������������ 163
Watch App Bundle�������������������������������������������������������������������������������������������������������� 165
Watch Extension����������������������������������������������������������������������������������������������������������� 166
vii
■ Contents
Index��������������������������������������������������������������������������������������������������������������������� 211
viii
About the Author
ix
About the Technical Reviewer
xi
Acknowledgments
Dear reader, you hold or view this book thanks to Joan Murray, who was very encouraging of my book
proposal and provided a lot of writing tips.
I am grateful to Chaim Krause for providing a very detailed technical review and catching even the
smallest mistakes.
Many thanks go to Laura Berendson for reading the manuscript, providing feedback, and giving general
advice. I thank Jill Balzano for her patience and keeping track of the book project. I also thank Welmoed
Spahr and Todd Green for publishing this book and April Rondeau for copyediting.
Finally, special thanks go to my wife, Agnieszka, daughter, Susanna, and son, Xavier, for their
continuous support and patience shown to me during the writing of this book.
xiii
Introduction
The programming of mobile apps has recently become one of the most important and exciting aspects of
the IT market, leading to numerous applications. At the same time, mobile development is very challenging
because of market fragmentation, which is manifested by the presence of devices of different sizes, operating
systems, and software development tools. So, to start doing mobile development, you need to take into
account these various factors and decide your strategy.
There are three leading mobile platforms: Apple (including iOS, watchOS, and tvOS), Android, and
Universal Windows Platform (UWP), each of which provides dedicated tools for developing apps. Each
platform has specific hardware requirements. Clearly, iOS SDKs require you to use the local Mac machine
or the remote build agent installed on a Mac. UWP apps can be built on Windows 10 machines. Finally, you
can use either Mac or Windows machines to develop Android apps. So, first and foremost, the question to
answer is that of which hardware to choose. For mobile development, the Intel-based Mac platform is the
best choice because it allows you to use macOS and Windows 10 concurrently. The latter can be installed
natively through the Boot Camp or virtually using the Parallels Desktop. Accordingly, Mac machines will give
you the most flexibility.
After choosing the hardware, you need to select the software development strategy. You have three
traditional options here:
• Native apps – In this case, you use platform SDKs, which require you to utilize
platform-specific IDEs and programming languages. For instance, in terms of iOS,
watchOS, and tvOS, you would need to use either Swift or Objective-C along with
Xcode. Native tools give you full access to the platform API at the cost of needing to
learn platform-specific programming languages.
• Hybrid apps – They are programmed with web technologies as web pages. These
pages are then rendered with a native WebView component, which can be virtually
understood as the local web browser, delivered by each platform. In this case, you
have a lot of flexibility in terms of programming tools and can use the same code
across various platforms at the cost of reduced access to platform-specific APIs.
• Mobile web apps – They are web apps whose views are tailored to mobile
devices. Such a strategy is the simplest to apply but does not let you access device
components, and it also requires a network connection.
Of course, development strategy is dictated by a number of factors, ranging from your programming
preferences to application complexity and target platforms. If you intend to target multiple platforms at
the same time, you can choose one of the cross-platform mobile tools, like Xamarin.Forms, Qt Mobile,
Embarcadero RAD Studio, or React Native. They provide an additional layer of the unified, platform-
independent programming API, which is translated to a platform-specific API during compilation.
Importantly, cross-platform instruments provide not only access to the API but also to visual controls. For
instance, if you create a message dialog, it will be converted during compilation to the platform-specific
message dialog. As a result, in such a “Write once, run anywhere (WORA)” approach, you can indeed
compile the same code across various platforms at the cost of flexibility. Finally, if your goal is to extensively
utilize platform-specific APIs, you will choose Xamarin.iOS or Xamarin.Android. These let you develop
xv
■ Introduction
native apps with C# or F#. Xamarin.iOS and Xamarin.Android provide an additional thin layer that maps C#
or F# code onto the native platform API. You typically use Xamarin.iOS and Xamarin.Android to implement
logic and design the UI separately for each platform. Although you have the extra work of creating platform-
specific UIs, you can still share the platform-independent code between various apps. Such an approach
gives you two important advantages:
• Your app is native. So, it looks like any other built-in app, as it utilizes native visual
controls.
• Your app is written in modern programming language. As a result, you have easy
access to numerous libraries and a wide community. This is especially important
when you are C# programmer who wants to start developing mobile apps.
In this book, we will learn how to use Xamarin.iOS to develop apps for Apple devices: iPhone and iPad
(iOS), Apple Watch (watchOS), and Apple TV (tvOS). We will first prepare the development tools (Visual Studio
for Mac) and will get to know available project templates (Chapter 1). Subsequently, I will explicitly show how
Xamarin.iOS is related to the native SDKs delivered by Apple, and we will investigate the app structure and
lifecycle (Chapter 2). Then, we will learn how to create views (Chapter 3) and implement navigation between
them (Chapter 4). Afterward, we will work with touch gestures (Chapter 5), study how to achieve high-quality
apps with automatic testing (Chapter 6), and consume data from RESTful web services (Chapter 7). Finally,
we will learn how to develop apps for Apple Watches (Chapter 8) and Apple TVs (Chapter 9).
In this book, however, we will not learn how to reuse code between various platforms nor how to
develop WORA apps with Xamarin.Forms. You can find more information about this in this book Beginning
Visual Studio for Mac: Build Cross-Platform Apps with Xamarin and .NET Core by Alessandro Del Sole.
xvi
CHAPTER 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 1-1, 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 platform–specific 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.
2
Chapter 1 ■ Fundamentals
Visual Studio installer will now verify your system configuration (Figure 1-4). More specifically, it looks
for installed components (like Mono Framework, Java SDK, and so on) in order to verify which of them have
to be downloaded and installed. Once this is done, another dialog appears on top of the window shown in
Figure 1-4. Its header tells you “Thank you for downloading Visual Studio.” In this dialog, you simply press
the Continue button.
3
Chapter 1 ■ Fundamentals
At this point, the Visual Studio installer might prompt you to install Xcode (Figure 1-5). 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.
4
Chapter 1 ■ Fundamentals
To install Xcode, you can press the Get Xcode button shown in Figure 1-5. This will direct you to a
website, where you click the View in Mac App Store or Install App button. It opens the Xcode page in the
App Store, on which you only need to click the Install App button (Figure 1-6). 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.
5
Chapter 1 ■ Fundamentals
The Visual Studio installer will now let you choose which components to install (Figure 1-7). 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.
6
Chapter 1 ■ Fundamentals
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 1-8. 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.
7
Chapter 1 ■ Fundamentals
8
Chapter 1 ■ Fundamentals
9
Chapter 1 ■ Fundamentals
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 1-1.
• Master-Detail App — This template creates apps that use Master-Detail interface. In
such cases, a list displays short descriptions of objects. Once you choose an object from
this list (master), corresponding details will be displayed in the dedicated area (detail).
Master-Detail interface is used, for example, in the Stocks iOS app (Figure 1-11).
10
Chapter 1 ■ Fundamentals
Figure 1-11. Representational view of a Master-Detail iOS app. The list of objects (companies) is shown on
top. When you tap any of these objects, corresponding details (stock values) appear at the bottom.
• Tabbed App — You use this template to create a multi-tab application in which you
can arrange visual controls in multiple tabs. User then navigates between tabs using
labeled icons shown at the botton of the screen. Such navigation is used in the Clock
iOS app (Figure 1-12).
11
Chapter 1 ■ Fundamentals
Figure 1-12. An example of Tabbed iOS application. You switch between tabs using labeled icons displayed in
the bottom part of the view
• Page-based App — Use this template to create a multi-view app in which controls are
arranged in pages. The user swipes between pages with touch gestures. For instance,
such page-based navigation is utilized in the Weather iOS app, shown in Figure 1-13.
12
Chapter 1 ■ Fundamentals
Figure 1-13. Page-based navigation is utilized in the Weather app to switch between weather forecasts
• WebView App — You use this template to jumpstart hybrid app development. This
project template creates a view with an embedded WebView control. The latter
renders a website written with HTML, CSS, and JavaScript. I will tell you more about
WebView in Chapter 3.
To proceed further, let’s pick the Single View app project template and keep the default language
selection of C#. Then, you will have the option to specify the name of your app and organization identifier
(Figure 1-14). I set these values to HelloWorld and com.db, respectively. The next group of controls in app
configuration lets you specify which devices will be supported by your app. In this case, I made my app
universal and chose iPad and iPhone. The very last control in the app configuration screen is the Target
drop-down list. You use this list to select the minimum iOS version that will be supported by your app. I
set this to iOS 9.0. After configuring the app, press the Next button, which activates the view depicted in
Figure 1-15.
13
Chapter 1 ■ Fundamentals
The Project Summary window shown in Figure 1-15 displays the project and solution names. You can
also use this screen to set the location for your source code, enable version control, and add an automated
UI test project. Here, I keep the settings at their default values. I will tell you more about unit testing in
Chapter 6. So, go ahead and press the Create button to proceed further. You will quickly see the Getting
Started screen of Visual Studio (Figure 1-16).
14
Chapter 1 ■ Fundamentals
The Getting Started screen displays several options for you. Specifically, it lets you start designing
the user interface (UI) of your app, add a mobile backend, and unit test the project. Also, on the left-hand
side of this window you will see the Solution Explorer, which displays the structure of the HelloWorld app.
Specifically, there is a HelloWorld solution, under which you can find the project of the same name (refer to
Figure 1-15). This project contains the files that make up your app. They are discussed in more detail in the
next chapter. For now, let’s create a simple UI for the HelloWorld app using the Storyboard (or iOS) Designer.
15
Chapter 1 ■ Fundamentals
Storyboard Designer
To start creating the UI, you can click the Open Storyboard Designer button. This will open the Main.
storyboard file and activate the Visual Interface or Storyboard Designer (or simply iOS Designer) of Visual
Studio. This designer is shown in Figure 1-17. Three elements of this designer should be discussed in more
detail, as follows:
• Visual preview of the app. This element covers the central part of the designer
and lets you see how your views will be displayed in the device without your
having to run your app. For multi-view apps, the visual preview will also depict the
relationships between views (tabs or pages) through which the user can navigate.
• Toolbox, which is in the top-right corner, contains a list of visual objects you can drag
on the view.
• Properties window, located below the Toolbox. You use the Properties window to
change the appearance of and configure visual controls, to define the layout, and to
wire methods to events (like tap or input) fired by controls.
Note that you can also activate the preceding windows or pads using an appropriate menu option in
Visual Studio: View ➤ Pads (see Figure 1-18). There is also one useful drop-down list in the preview mode.
That is the View As list, which you can find on the top pane of the visual preview. The View As drop-down list
lets you choose the device type to use for the preview. As shown in Figure 1-17, I set this device to iPhone 6.
16
Exploring the Variety of Random
Documents with Different Content
Fig. 26. Another type of dwelling house.
Fig. 30. Buried in trees. The opposite extreme from Fig. 26.
“It is a plain, rugged, austere structure, like the men who built it,
and any proposal to modernize it would be received with disfavor; for
it means more to the people than merely a church building—it is a
sacred possession that is a part of their life,” and it is an appropriate
monument to the sturdy religious character of the pioneers who
stood in the forefront as a wall guarding human rights and liberties in
those stormy days of the past. The country church should be as truly
a part of the farm structure as are the house and barn, located on
land held in fee simple.
Fig. 32. Where horses are kept.
Fig. 33. Where boys and girls are taught.
Most of the farmers who now occupy the country west of the
Alleghanies came from the east and brought with them a varied
assortment of styles of architecture inherited from the many
European countries from which they or their ancestors came. These
people, though of limited means, had pride and tenacity of purpose,
and they could not easily change to the plain and appropriate
exterior treatment of the farm house. This inheritance and
persistence, as shown in the farm houses of the middle states, is fitly
illustrated by the expensive and heavy return cornice, the massive
columns, and the complicated and ornate entablatures which are
supposed to adorn an otherwise plain house.
Fig. 36. The expensive box cornice.
They may be very unlike, yet both beautiful. From the farmer’s
standpoint it may be said that the chief characteristics of beauty are
fitness, naturalness and simplicity.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com