Table Of ContentBeginning Xamarin
Development for
the Mac
Create iOS, watchOS, and Apple tvOS apps
with Xamarin.iOS and Visual Studio for Mac
—
Dawid Borycki
Beginning Xamarin
Development for
the Mac
Create iOS, watchOS, and Apple tvOS apps
with Xamarin.iOS and Visual Studio for Mac
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 orders-ny@springer-sbm.com, 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 rights@apress.com, 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
About the Author �����������������������������������������������������������������������������������������������������ix
About the Technical Reviewer ���������������������������������������������������������������������������������xi
Acknowledgments �������������������������������������������������������������������������������������������������xiii
Introduction �������������������������������������������������������������������������������������������������������������xv
■ 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
Map View and Geolocation ���������������������������������������������������������������������������������������������68
Auto-Layout ��������������������������������������������������������������������������������������������������������������������73
Size Classes �������������������������������������������������������������������������������������������������������������������77
UI Thread ������������������������������������������������������������������������������������������������������������������������80
Summary ������������������������������������������������������������������������������������������������������������������������82
■ Chapter 4: Navigation ������������������������������������������������������������������������������������������83
Tab Bar ���������������������������������������������������������������������������������������������������������������������������83
Pages ����������������������������������������������������������������������������������������������������������������������������������������������������90
Navigation Between View Controllers ���������������������������������������������������������������������������������������������������95
Editing a Segue �������������������������������������������������������������������������������������������������������������������������������������97
Unwind Segue ��������������������������������������������������������������������������������������������������������������������������������������98
Preparing for Segues ����������������������������������������������������������������������������������������������������������������������������99
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
Pan Gesture Recognizer ���������������������������������������������������������������������������������������������������������������������107
Detecting Gesture Location ����������������������������������������������������������������������������������������������������������������109
Rotation and Pinch Gesture Recognizers ��������������������������������������������������������������������������������������������111
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
Xamarin Test Cloud �������������������������������������������������������������������������������������������������������135
Provisioning Profile �����������������������������������������������������������������������������������������������������������������������������136
Running Tests in the XTC ��������������������������������������������������������������������������������������������������������������������140
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
Hello, Watch!�����������������������������������������������������������������������������������������������������������������166
Watch Simulator �����������������������������������������������������������������������������������������������������������169
View Lifecycle ��������������������������������������������������������������������������������������������������������������171
App Lifecycle ����������������������������������������������������������������������������������������������������������������173
Text Input ����������������������������������������������������������������������������������������������������������������������175
Force Touch and Navigation �����������������������������������������������������������������������������������������177
Notification Controller ��������������������������������������������������������������������������������������������������181
ClockKit and Complication Controller ���������������������������������������������������������������������������184
Glance Controller ����������������������������������������������������������������������������������������������������������189
Summary ����������������������������������������������������������������������������������������������������������������������191
■ Chapter 9: tvOS ��������������������������������������������������������������������������������������������������193
Creating a Project ���������������������������������������������������������������������������������������������������������194
User Interface ���������������������������������������������������������������������������������������������������������������195
OpenWeatherMap API ���������������������������������������������������������������������������������������������������197
Retrieving the Weather Report �������������������������������������������������������������������������������������199
Presenting the Weather ������������������������������������������������������������������������������������������������202
Temperature Units ��������������������������������������������������������������������������������������������������������205
Testing the App in a Simulator �������������������������������������������������������������������������������������206
Summary ����������������������������������������������������������������������������������������������������������������������209
Index ���������������������������������������������������������������������������������������������������������������������211
viii
About the Author
Dawid Borycki is a software engineer, biomedical researcher, and an
expert in several Microsoft developer technologies. He has resolved a
broad range of software development challenges for device prototypes
(mainly medical equipment), embedded device interfacing, and desktop
and mobile programming. Dawid regularly speaks at international
developers conferences and has published, cited, and presented on
numerous developer topics, including web technologies, mobile/cross-
platform development, wearables, embedded, and more.
ix
About the Technical Reviewer
Chaim Krause is an expert computer programmer with over thirty years
of experience to prove it. He worked as a lead tech support engineer for
ISPs as early as 1995 and as a senior developer support engineer with
Borland for Delphi, and has worked in Silicon Valley for over a decade
in various roles, including technical support engineer and developer
support engineer. He is currently a military simulation specialist for the
US Army’s Command and General Staff College, working on projects such
as developing serious games for use in training exercises. He has also
authored several video training courses on Linux topics and has been
a technical reviewer for over twenty books, including iOS Code Testing,
Android Apps for Absolute Beginners (4th ed.), and XML Essentials for C#
and .NET Development (all Apress). It seems only natural then that he
would be an avid gamer and have his own electronics lab and server room in his basement. He currently
resides in Leavenworth, Kansas, with his loving partner, Ivana, and a menagerie of four-legged companions:
their two dogs, Dasher and Minnie, and their three cats, Pudems, Talyn, and Alaska.
xi
Description:Develop apps for the iPhone, iPad, and Apple wearables using Visual Studio for the Mac. Learn how to set up your development environment and emulators, and how to create adaptive user interfaces for various platforms. Expert Dawid Borycki guides you through the fundamentals of programming for Apple