Tải bản đầy đủ (.pdf) (462 trang)

Program the Internet of Things with Swift for iOS Learn How to Program Apps for the Internet of Things by Ahmed Bakir

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (6.35 MB, 462 trang )

Program the
Internet of Things
with Swift for iOS
Learn How to Program Apps for the
Internet of Things

Second Edition

Ahmed Bakir


Program the Internet of
Things with Swift for iOS
Learn How to Program Apps for the
Internet of Things
Second Edition

Ahmed Bakir


Program the Internet of Things with Swift for iOS: Learn How to Program Apps for
the Internet of Things
Ahmed Bakir
devAtelier, Tokyo, Japan
ISBN-13 (pbk): 978-1-4842-3512-6
/>
ISBN-13 (electronic): 978-1-4842-3513-3

Library of Congress Control Number: 2018964570

Copyright © 2018 by Ahmed Bakir


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 author 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.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Aaron Black
Development Editor: James Markham
Coordinating Editor: Jessica Vakili
Cover image designed by Freepik (www.freepik.com)
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 , 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 , or visit 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 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/978-1-4842-3512-6. For more
detailed information, please visit www.apress.com/source-code.
Printed on acid-free paper


Dedicated to my mother, Layla Bakir, who shared with me her love of
teaching and enduring optimism


Table of Contents
About the Author����������������������������������������������������������������������������������������������������� xi
About the Technical Reviewer������������������������������������������������������������������������������� xiii
Acknowledgments���������������������������������������������������������������������������������������������������xv
Introduction�����������������������������������������������������������������������������������������������������������xvii

Part 1: Building Health Apps for the Internet of Things���������������������������������� 1
Chapter 1: Laying the Foundation for Your First IoT App������������������������������������������ 3
Learning Objectives���������������������������������������������������������������������������������������������������������������������� 4
Setting Up the Project������������������������������������������������������������������������������������������������������������������� 5
Linking Your Apple Developer Account to Xcode������������������������������������������������������������������� 13
Building an Adaptive User Interface�������������������������������������������������������������������������������������������� 17
Renaming Classes from the Base Template�������������������������������������������������������������������������� 19
Laying Out the User Interface������������������������������������������������������������������������������������������������ 22
Applying Auto Layout Constraints������������������������������������������������������������������������������������������ 24
Customizing the Appearance of Items����������������������������������������������������������������������������������� 27
Resolving Auto Layout Issues������������������������������������������������������������������������������������������������ 37
Connecting the Storyboard to Your Code������������������������������������������������������������������������������������ 41
Defining Interface Builder-Compatible Properties and Methods (Actions)���������������������������� 42
Using the Connection Inspector to Make the Final Storyboard Connections������������������������� 45

Summary������������������������������������������������������������������������������������������������������������������������������������ 50

v


Table of Contents

Chapter 2: Using Core Location to Build a Workout Tracking App�������������������������� 51
Learning Objectives�������������������������������������������������������������������������������������������������������������������� 52
Configuring Your Project for Background Location Activity��������������������������������������������������������� 52
Requesting Location Permission������������������������������������������������������������������������������������������������� 58
Checking for Hardware Availability���������������������������������������������������������������������������������������� 62
Responding to Changes in Location Permission Status�������������������������������������������������������� 64
Requesting Location Updates����������������������������������������������������������������������������������������������������� 72
Responding to Location Updates������������������������������������������������������������������������������������������� 74
Displaying Location Data on a Map��������������������������������������������������������������������������������������������� 81
Using the Codable Protocol for File-Based Data Storage������������������������������������������������������ 82
Displaying Saved Locations on a Map���������������������������������������������������������������������������������������� 93
Summary������������������������������������������������������������������������������������������������������������������������������������ 96

Chapter 3: Using Core Motion to Add Physical Activity Data to Your Apps������������ 97
Learning Objectives�������������������������������������������������������������������������������������������������������������������� 98
Requesting Motion Permission from the User���������������������������������������������������������������������������� 99
Receiving Real-Time Step Count Updates from the iPhone’s Pedometer��������������������������������� 104
Updating the User Interface������������������������������������������������������������������������������������������������� 108
Stopping and Pausing Pedometer Updates������������������������������������������������������������������������� 111
Getting Activity Type������������������������������������������������������������������������������������������������������������������ 113
Handling Altimeter Updates������������������������������������������������������������������������������������������������������ 118
Summary���������������������������������������������������������������������������������������������������������������������������������� 121


Chapter 4: Using HealthKit to Securely Retrieve and Store Health Data��������������� 123
Learning Objectives������������������������������������������������������������������������������������������������������������������ 124
Requesting HealthKit Permission���������������������������������������������������������������������������������������������� 125
Writing Data to HealthKit����������������������������������������������������������������������������������������������������������� 132
Understanding How HealthKit Represents Data������������������������������������������������������������������ 132
Creating and Saving HealthKit Samples������������������������������������������������������������������������������ 133
Reading Workout Data from HealthKit�������������������������������������������������������������������������������������� 143
Using a Table View Controller to Display Data��������������������������������������������������������������������� 146
Summary���������������������������������������������������������������������������������������������������������������������������������� 160
vi


Table of Contents

Part 2: Building Your Own Internet Things������������������������������������������������� 161
Chapter 5: Building Arduino-Based Peripherals��������������������������������������������������� 163
Learning Objectives������������������������������������������������������������������������������������������������������������������ 164
Building the Wireless Door-Sensor Hardware��������������������������������������������������������������������������� 166
Part List������������������������������������������������������������������������������������������������������������������������������� 166
Assembling the Hardware��������������������������������������������������������������������������������������������������� 168
Writing an Arduino Solution (Program)�������������������������������������������������������������������������������������� 178
Setting Up the Arduino Programming Environment������������������������������������������������������������� 179
Using GPIO to Monitor Input Pins and Control Output Pins�������������������������������������������������� 184
Calculating Battery Status��������������������������������������������������������������������������������������������������� 187
Running and Monitoring the Arduino Solution��������������������������������������������������������������������� 188
Summary���������������������������������������������������������������������������������������������������������������������������������� 191

Chapter 6: Building a Bluetooth LE Hardware Companion App���������������������������� 193
Learning Objectives������������������������������������������������������������������������������������������������������������������ 193
A Quick Introduction to Bluetooth LE���������������������������������������������������������������������������������������� 195

Adding Bluetooth Functionality to an Arduino Solution������������������������������������������������������������� 197
Installing the ESP32_BLE_Arduino Library for Bluetooth Communication�������������������������� 198
Setting Up the Arduino As a Bluetooth Peripheral��������������������������������������������������������������� 201
Sending Data Updates via Bluetooth LE������������������������������������������������������������������������������ 208
Using Core Bluetooth to Communicate with Bluetooth LE Devices������������������������������������������� 210
Setting Up the IOTHome Project������������������������������������������������������������������������������������������ 211
Setting Up the App As a Central Manager��������������������������������������������������������������������������� 216
Connecting to a Bluetooth LE Peripheral����������������������������������������������������������������������������� 222
Monitoring Characteristic Updates�������������������������������������������������������������������������������������� 226
Monitoring Updates While the App Is in the Background���������������������������������������������������� 229
Summary���������������������������������������������������������������������������������������������������������������������������������� 233

vii


Table of Contents

Chapter 7: Setting Up a Raspberry Pi and Using It As a HomeKit Bridge������������� 235
Learning Objectives������������������������������������������������������������������������������������������������������������������ 236
Setting Up the Raspberry Pi HomeKit Bridge���������������������������������������������������������������������������� 237
Putting Together the Hardware�������������������������������������������������������������������������������������������� 237
Bootstrapping the Raspberry Pi������������������������������������������������������������������������������������������� 241
Installing HomeBridge��������������������������������������������������������������������������������������������������������� 248
Configuring HomeBridge to Read Data from a Temperature Sensor����������������������������������� 253
Configuring HomeBridge to Connect to a Bluetooth LE Accessory�������������������������������������� 255
Connecting to Your New HomeKit Bridge���������������������������������������������������������������������������� 261
Summary���������������������������������������������������������������������������������������������������������������������������������� 266

Chapter 8: Building a Web Server on a Raspberry Pi������������������������������������������� 267
Learning Objectives������������������������������������������������������������������������������������������������������������������ 268

Creating a Web Server to Share Data over HTTPS�������������������������������������������������������������������� 269
Using Express to Expose Web Services������������������������������������������������������������������������������� 269
Reading Values from the DHT Temperature Sensor������������������������������������������������������������� 273
Reading Information from Bluetooth Devices���������������������������������������������������������������������� 276
Using HTTPS to Provide Secure HTTP Connections������������������������������������������������������������� 283
Configuring the Server to Start Up with the Raspberry Pi��������������������������������������������������� 290
Connecting to Your Server from an iOS App������������������������������������������������������������������������������ 292
Setting Up the User Interface���������������������������������������������������������������������������������������������� 292
Making and Responding to HTTPS Requests����������������������������������������������������������������������� 297
Summary���������������������������������������������������������������������������������������������������������������������������������� 309

Part 3: Building Apps Using Apple’s Advanced Internet of
Things Technologies����������������������������������������������������������������������������������� 311
Chapter 9: Using tvOS to Build an Apple TV Dashboard App�������������������������������� 313
Learning Objectives������������������������������������������������������������������������������������������������������������������ 314
Setting Up the tvOS Target�������������������������������������������������������������������������������������������������������� 315
Creating the User Interface������������������������������������������������������������������������������������������������������� 319
Programmatically Styling Elements to Match the tvOS Design Language�������������������������� 324
Using Font Awesome for Font-Based Graphics������������������������������������������������������������������� 328
viii


Table of Contents

Adding Data Sources to the tvOS App��������������������������������������������������������������������������������������� 332
Requesting User Location���������������������������������������������������������������������������������������������������� 335
Connecting to the OpenWeatherMap API����������������������������������������������������������������������������� 339
Handling Touch Input from the Siri Remote������������������������������������������������������������������������������ 351
Debugging the App on an Apple TV������������������������������������������������������������������������������������������� 354
Summary���������������������������������������������������������������������������������������������������������������������������������� 358


Chapter 10: Using watchOS to Build an Apple Watch App������������������������������������ 359
Learning Objectives������������������������������������������������������������������������������������������������������������������ 360
Setting Up the Project��������������������������������������������������������������������������������������������������������������� 361
Building a watchOS User Interface������������������������������������������������������������������������������������������� 368
Setting Up a Table View Using the WKInterfaceTable Class������������������������������������������������� 380
Adding Force Touch Support������������������������������������������������������������������������������������������������ 383
Creating a New Workout Using Core Location and Core Motion����������������������������������������������� 388
Using HealthKit to Populate the Workout History Table������������������������������������������������������������� 401
Summary���������������������������������������������������������������������������������������������������������������������������������� 405

Chapter 11: Using Face ID, Touch ID, and Keychain Services to
Secure Your Apps������������������������������������������������������������������������������������������������� 407
Learning Objectives������������������������������������������������������������������������������������������������������������������ 408
Setting Up the Project��������������������������������������������������������������������������������������������������������������� 409
Creating a Lock Screen User Interface�������������������������������������������������������������������������������� 410
Querying for Sensor Availability������������������������������������������������������������������������������������������������ 421
Using Face ID or Touch ID to Restrict Access to Features��������������������������������������������������������� 424
Using Keychain Services to Secure Data���������������������������������������������������������������������������������� 430
Using Biometrics or an App Password to Lock Keychain Items������������������������������������������ 437
Detecting When an App Returns to the Foreground������������������������������������������������������������������ 441
Summary���������������������������������������������������������������������������������������������������������������������������������� 443

Index��������������������������������������������������������������������������������������������������������������������� 445

ix


About the Author
Ahmed Bakir is an iOS author, teacher, and entrepreneur. After starting his career as

a firmware engineer, he made the mistake of telling someone at a party that he was
developing iPhone apps for fun and has been inundated with work ever since. He has
worked on more than 30 mobile projects, ranging from advising startups to architecting
apps for Fortune 500 companies. In 2014, he published his first book, Beginning iOS
Media App Development, followed, in 2016, by the first edition of Program the Internet of
Things with Swift for iOS. In 2015, he was invited to develop and teach iOS development
at UCSD-Extension. He is currently building cool stuff in Tokyo, Japan! You can find him
online at www.devatelier.com.

xi


About the Technical Reviewer
Massimo Nardone has more than 22 years’ experience in security, web/mobile
development, and cloud and IT architecture. His true IT passions are security and
Android. He has been programming and teaching how to program with Android, Perl,
PHP, Java, VB, Python, C/C++, and MySQL for more than 20 years.
Massimo holds a master of science degree in computer science from the University
of Salerno, Italy. He has worked as a project manager, software engineer, research
engineer, chief security architect, information security manager, PCI/SCADA auditor,
and senior lead IT security/cloud/SCADA architect for many years. Among his technical
skills are security, Android, cloud, Java, MySQL, Drupal, Cobol, Perl, web and mobile
development, MongoDB, D3, Joomla, Couchbase, C/C++, WebGL, Python, Pro Rails,
Django CMS, Jekyll, and Scratch.
Massimo currently works as chief information security officer at Cargotec Oyj.
He is a visiting lecturer and supervisor for exercises at the Networking Laboratory
of the Helsinki University of Technology (Aalto University), Finland. He holds four
international patents (related to PKI, SIP, SAML, and Proxy).

xiii



Acknowledgments
Every author writes a book to tell a story, whether to entertain, educate, or provide an
experience for the reader that lies somewhere in between. What I have learned from
writing these past few years is that the process itself also has a story. The process of
writing this book had a more elaborate story than my first two, but on this page, I’d like to
thank as many people as possible who had a part in it.
First and foremost, I would like to thank my editors, Jessica Vakili and Aaron Black,
whose incredible patience and support allowed me to complete this work. I truly
apologize for any headaches I may have given you, but I am really proud of what we
produced together.
Next, I would like to thank my original coauthors on the first edition of this book,
Gheorghe Chesler and Manny de la Torriente. Beyond writing together, our experiences
while collaborating greatly shaped me as a developer and provided many fond memories
that I still look back on fondly today. The first edition of this book is something I will
always be proud of, especially because we produced it at a time when so much of Swift
and the Internet of Things was still up in the air!
Finally, I must thank my incredible family and friends, who have always supported
me, even on my crazy adventure of moving to Japan. They helped me realize that I was
seeking a new challenge, and they constantly encouraged me to persevere through every
part of it. Thank you, all!
To everyone I may have missed, don’t forget the legend of Hana no Asuka-gumi, and
I’ll be back again soon for our next misadventures at the Bonaventure!

xv


Introduction
Welcome back to the Internet of Things (IoT). When my original coauthors, Gheorghe

Chesler and Manny de la Torriente, and I wrote the first edition of this book in 2016, we
combined what we learned from our careers, consulting projects, and dreams to put
together a narrative we hoped would help future developers write their own IoT apps for
iOS, using Swift. Looking at the Apress web site today, it appears that more than 30,000
people have read the book through their site alone, so I am glad we were able to reach
out to at least a few readers!
However, time marches on, and since the publication of the first edition, Swift and
the Internet of Things have evolved considerably. Some of the technologies we originally
wanted to write about have finally become real, and others have fallen by the wayside.
Most important, Swift as a language has finally begun to stabilize, and the developer
community has been establishing a clearer set of design patterns and coding standards
that are appropriate for the language. In addition to the Swift version from the original
edition (2.0) being incompatible with modern versions of the language, the time was
right to overhaul the original book.
This edition strikes a balance between revisiting some of the most loved content
from the first edition and presenting new concepts that were not available when the
previous edition was published. I have eliminated or streamlined concepts that fizzled
out and greatly expanded on those that have proven to be more important than before.
In particular, this book offers more coverage on building hardware projects, as the
availability of high-quality, affordable parts has expanded rapidly, as has their ease
of use. Beyond this, I noticed that, in general, more people want to build their own
hardware, and apps to go along with it.
Additionally, I have tried to make this edition accessible to more readers. While
every book I have written has been a narrative, aimed at developers with a basic
understanding of iOS development, in this volume, I have modified the format to reflect
the feedback I received while teaching and speaking to readers. Namely, this edition

xvii



Introduction

allows you to skip around more between topics and provides more detailed step-by-step
instructions. Rather than simply giving you code to copy, I try to explain what you need
to put together to accomplish each task in a project. My hope is that this will allow you to
debug problems easier and use this book beyond the initial Swift version it was written
for (Swift 4.2).
While it would be impossible to describe the IoT fully in one paragraph, in this book,
I focus on how you can build, interact with, and network hardware-based sensors to
make your users’ lives more informed and convenient. In my home, I am able to use
IoT devices to turn off my lights with my voice, determine if I am using my appliances at
times when power is too expensive, and to figure out if I am brewing my tea at the perfect
temperature (I am, but I need to remember to get up when the timer goes off ). In this
book, some of the projects you will implement will help you to keep better track of your
workouts, determine whether you have locked your door, and show you a dashboard for
your home on your Apple TV. Pretty cool for do-it-yourself projects!
Finally, remember, this book exists for you. The ultimate goal of writing it was to help
you and the developer community gain a stronger understanding of IoT development as
it relates to iOS and provide a wealth of inspiration for your own future projects. If you
would like to contribute to making sure that the code stays up to date, please submit pull
requests to the GitHub repository for the book ( />internet-­of-things-w-swift-for-ios). If you have any feedback, I would be excited to
hear it via Apress or my web site (www.devatelier.com). Happy hacking!

xviii


PART 1

Building Health Apps
for the Internet of Things



CHAPTER 1

Laying the Foundation
for Your First IoT App
Before taking you on a deep dive into Apple’s Internet of Things (IoT) technologies,
I thought it would be useful to begin with a brief introduction to some of the tools
and workflows you will use throughout the book to build your projects. In this book,
your primary tools for building IoT apps will be the Xcode Integrated Development
Environment from Apple and its Interface Builder tool for building user interfaces.
When teaching iOS development, I always notice that both novice and experience
developers find these tools to encompass some of the most challenging concepts to
master, even more than Swift or the eccentricities of any framework.
To practice using Xcode, in this chapter, you will begin developing your first IoT app
for iOS: IOTFit, a workout app that uses the location services (GPS) on a user’s phone
to help them keep track of how long they exercised and where. If you are an avid user
of such workout apps as Runkeeper or Nike Plus, you will recognize time tracking as a
minimum requirement and location tracking as a feature that engages users and keeps
them coming back for more.
Throughout this book, you will continue to expand the IOTFit app to integrate more
of Apple’s IoT frameworks. I hope this approach will help you to notice when some
solutions are more appropriate than others. Additionally, I feel this approach is very
reminiscent of many Agile working environments, in which you constantly refine or
expand a product, based on one strongly defined feature set at a time.
If you are already familiar with iOS development, you can safely skim through this
chapter, but I highly recommend glancing through the screenshots to see if anything has
changed since you last worked with Xcode. This chapter has been designed and tested
for Xcode 9 and 10.


© Ahmed Bakir 2018
A. Bakir, Program the Internet of Things with Swift for iOS, />
3


Chapter 1

Laying the Foundation for Your First IoT App

Learning Objectives
In this chapter, you will learn the following critical skills for IoT development on iOS by
starting development on the IOTFit application.


Setting up a project in Xcode



Modifying a project from its default settings



Linking Xcode to your Apple Developer Program account



Using Interface Builder to lay out an adaptive user interface




Connecting your visual layout-based user interface to your code

One of the most aggravating points of Apple platform development is that you
have to master certain workflows and aspects of using Xcode in order to achieve
critical milestones as an iOS developer, such as releasing your first app. To help you
with this process, in this chapter, I will try to include as many detailed screenshots and
explanations as possible for the project and user interface setup steps.
Unfortunately, Apple frequently replaces its old tools and workflows. This allows
Apple to build tools that are more relevant to current trends, but it also places a burden
on developers to keep up to date with the latest changes and caveats. In my personal
experience, I have noticed that while a workflow may change only every few years, the
project settings change with almost every major release. In the past two years, iOS 11
added mandatory settings for enabling permission-locked features like location sharing
and iOS 12 went even further by replacing the default build system.
In this chapter, I emphasize adaptive (Apple’s term) user interface development,
because the current range of supported iOS devices is so vast. While devices with the
form factor of the iPhone 8 and 8 Plus are the most widely used at the time of writing,
the tiny iPhone SE still makes up a huge segment of active devices, and Apple’s bezelless devices (iPhone X, XR, XS, XS Max) are being positioned strongly as the future of the
platform. If you take the iPad lineup into consideration, you will notice that the iPad mini
is nearly the same size as the iPhone 8 Plus, and the iPad Pro 13-inch model is larger
than many of today’s laptop computers. As a developer, it is amazing that the same code
can run on all of these devices; however, it comes at the cost of having to do some careful

4


Chapter 1

Laying the Foundation for Your First IoT App


preparation work and debugging, to make sure the experience is consistent across all
devices. In this chapter, I will share the workflow I use to build adaptive user interfaces
and tips I think will help make it easier for you.

Note  For the sake of clarity, the diagrams in this book illustrate the iPhone
implementation of these projects. Most of the projects in this book will run fine
on iPad as well, except for the ones that require device-specific hardware (for
example, Core Motion, Face ID). I will indicate what these projects are at the
beginning of the relevant chapter.
This chapter is intended to guide you through the process of setting up a project, but
if you run into any trouble along the way and would like to look at the completed project
for reference, it is available from the GitHub repository for this book, under the Chapter 1
folder ( />
S
 etting Up the Project
Before I start developing a project, I always want to know what I am building. To help you
gain a better understanding of what the first version of the IOTFit app will encompass,
please look at the wireframe diagrams in Figure 1-1. In design terms, a wireframe is
usually an initial sketch (hand-drawn or computer-generated) that lays out the most
critical components of a user interface.

5


Chapter 1

Laying the Foundation for Your First IoT App

Figure 1-1.  Wireframe diagrams for the IOTFit app
In reviewing the wireframes, you will notice three main states for the application:

recording a workout (inactive), recording a workout (active), and displaying a workout
on a map. When users want to start a workout, they press the Start button on the record
screen, and the text on the buttons and labels change to indicate that the workout is
being recorded. If users want to view their progress on a map, they press the Map icon
in the bottom tab bar, and the app will show an annotated map in place of the record
screen. If you use the App Store or Facebook apps for iOS, you will already be familiar
with the tab bar as a convenient way of switching screens in an app, while still preserving
the state of each tab. Although it is not listed on the wireframe, the background state
preservation you will implement for the app allows users to keep tracking a workout,
even when the app is in the background.

6


Chapter 1

Laying the Foundation for Your First IoT App

WIREFRAMES VS. MOCKUPS
I like to use wireframes at the beginning of a project to put the stakeholders, developers,
and design team on the same page about what an app has to do, before committing to the
time-consuming work of generating mockups—the Photoshop- or Sketch-generated design
resources that specify the fine details of implementation, including exact colors, font sizes, and
shadows. It is much easier to throw away or redo a wireframe than it is a mockup!
To begin the development process, you will have to set up a new Xcode project for the
IOTFit app and configure it for the iOS frameworks you will need to use. Apple provides
a very rich toolbox for you to work with, but it always requires some careful preparation.
Before getting started, take a second to think about what frameworks you would like to
use to implement the requirements of the app, then refer to Table 1-1 for the final list of
what application programming interfaces (APIs) you will end up using in this project.


Table 1-1.  IOTFit Features and Their Corresponding iOS APIs
Requirement

Application Programming Interface

Parent Framework

Switching between screens easily

Tab View Controller
(UITabViewController)

UIKit

Displaying a map

Map View
(MKMapViewController)

MapKit

Accessing GPS hardware

Location Manager
(CLLocationManager)

Core Location

Requesting location permission


Location Manager
(CLLocationManager)

Core Location

The UITabViewController and MKMapViewController classes will drive the most
complicated parts of the user interface. The Core Location framework will do the heavy
lifting for requesting and tracking the user’s location.

7


Chapter 1

Laying the Foundation for Your First IoT App

Now that you have a better idea of the technical and design aspects of the project,
you can begin implementation. First, create a new Xcode Project by opening Xcode on
your Mac and clicking Create a new Xcode project from the Welcome to Xcode screen
shown in Figure 1-2. Alternatively, if Xcode is already open, you can click the File menu
and then select New ➤ Project.

Figure 1-2.  Creating a new project from the Xcode welcome screen
Next, Xcode will present a pop-up window asking you to select the template type
for your project. As shown in Figure 1-3, select Tabbed App to create a project based
on a template that includes a Tab Bar Controller and two empty View Controllers. This
template is close to the general user interface you must use for the IOTFit app and saves
a lot of time over manually setting everything up yourself.


8


Chapter 1

Laying the Foundation for Your First IoT App

Figure 1-3.  Selecting the Tabbed App template
After selecting the correct template, click the Next button, then when asked to set the
options for the project, enter “IOTFit” as the project name, as shown in Figure 1-4.
This will be used as a general identifier throughout the project and as the default
display name of the app on the iOS home screen. If you have an organization name or
organization identifier you would like to use, you can enter those at this time too. You do
not have to set a team for the project at this time, as you will do that later, after verifying
that the project has been created successfully.

9


Chapter 1

Laying the Foundation for Your First IoT App

Figure 1-4.  Initial options for IOTFit project
After confirming these options, click the Next button, then select a location to save
the project. I like to place my projects in an easy-to-find folder in my home directory. As
shown in Figure 1-­5, click the Create button to generate the project.

10



Chapter 1

Laying the Foundation for Your First IoT App

Figure 1-5.  Selecting a destination for the IOTFit project
After the project has been generated successfully, Xcode will greet you with the
Xcode editor window for your new project, as shown in Figure 1-6.

11


Chapter 1

Laying the Foundation for Your First IoT App

Figure 1-6.  Default Xcode editor window for the IOTFit project
If you have been developing apps for a while, this editor window should be very
familiar to you. For newer users, the major areas of this screen are described following.


Navigator pane (left): The atlas for your project. This allows you to
manage your project hierarchy, search for text in your project, and
quickly navigate to debugging issues.



Editor pane (center): Your main editing workspace. This lets you
modify source code, build settings, and view diffs of source-controlmanaged files.




Utilities pane (right): Your source code concierge. With this, you can
manage additional settings for individual files and view quick help
tips on classes you are working with (by simply highlighting them).

Getting back to the project at hand, verify that the settings for the generated project
are similar to those in the zoomed-in screenshot in Figure 1-7. In particular, verify that
the project has source files in the navigator pane and that the app’s Display Name and
Bundle Identifier match what you entered into the previous screens.

12


×