Introduction

Ahmed Bakir

What Is the Internet of Things?

The Internet of Things refers to the push to make applications and hardware devices, or “things,” “smart” by receiving or logging data from the Internet and other things. The goal of the Internet of Things is to use these additional data sources to make common tasks in your life richer and easier to perform.

One of the earliest drivers of the Internet of Things was the Quantified Self movement, a trend that suggested people could lose weight and exercise at more sustainable levels by constantly logging and monitoring their diet and workout information. Although this started out with data gleaned from calorie-counting journals and pedometers, the creation of apps like MyFitnessPal, which would help you find caloric information for your afternoon snack, and devices like the FitBit, which automatically logged your pedometer data to the Internet, pushed Quantified Self into the mainstream.

Another example of the Internet of Things in action would be a smart TV.  Several years ago, your TV was a “dumb” screen, only displaying the output from the input devices you connected to it, such as a cable box, game console, or VCR (do you remember those?). Fast forwarding to the future, today’s TVs commonly include WiFi cards and “smart” application platforms that allow you to perform some of your most common tasks, such as streaming videos from Netflix or browsing photos from Instagram directly from the TV, without having to connect a computer. While many TVs are not yet at the point where they have “intelligent” features, such as suggesting cooking shows because you watch a lot of them, the hope is that having Internet connectivity and an application platform will inspire developers to program these applications for TVs.

A significant distinction between today’s Internet of Things and previous attempts to connect devices to the Internet is that the barrier of entry has dropped significantly. Whereas previously, the only way to build an Internet-connected device was to have a staff of highly trained hardware and software engineers building a proprietary platform for years, today you can go to any electronics store or web site and buy an Arduino or Raspberry Pi, which puts the motherboard of a computer from a few years ago in the palm of your hand for about $30. These devices were designed to give hobbyists and students an easy way to enter electronics (previously, it was a very expensive hobby—the voice of experience), but also include all the core features you need to build a connected device: a CPU, the ability to run high-level programming languages (such as Python), a WiFi card, a display port (generally HDMI), and a series of GPIO (general purpose input/ouptut) pins, which allow you to connect electronic components, such as timer chips and LED lights.

Consumers and companies have noticed the demand of connected devices, and the ease of entry into the market, making this the perfect time to learn how to program for the Internet of Things! Who knows, maybe your app will be the one that drives personalized toast into the mainstream (the best way of making sure people don’t steal your lunch)!

What Is the Purpose of This Book?

The purpose of this book is to teach you how to build iOS applications, in Swift, that use Apple’s native application programming interfaces (APIs), which connect to popular Internet of Things (IoT) devices and services. We have framed our narrative around the following four classes of devices:

  • Fitness and health trackers
  • Apple Watch
  • Generic hardware accessories
  • Authentication and payment systems

These device families represent some of the most popular classes of IoT accessories, while allowing us to teach you several different ways of connecting to IoT devices, including native iOS libraries (e.g., HealthKit and WatchKit), generic hardware interfaces (e.g., Bluetooth), third-party data logging services (e.g., FitBit), and local networking (over WiFi). The beauty of today’s IoT is that that there are so many ways of connecting your devices, based on widely adopted open standards, reducing your need to learn proprietary protocols. Our goal is that by exposing you to different ways of connecting to IoT devices, you will have a handy toolbox of skills that will cover most of the use cases you will be asked to implement.

This book is presented in a tutorial style, which takes cues from modern software engineering practices, such as code reviews and Agile programming. Each chapter in this book is framed around a project you will be learning how to implement, which will be described via requirements and “stories” indicating why they are important. In a similar manner, our explanations will go deep into the code, pulling in best practices from Apple’s specifications and other applications. Whenever necessary, we will pull in brief explanations of underlying topics, such as delegate programming and OAUTH authentication, so you do not need to keep flipping back and forth between different books. Our goal for this book is to give you a deep understanding of the projects you will be implementing, rather than copy-and-paste snippets. As anyone who has survived a major iOS upgrade (e.g., iOS6 to iOS7) can attest, knowing the core concepts helps you fix your code way faster than memorizing single-use snippets.

What Do I Need to Know to Use This Book?

This book is intended to serve as a guide to implementing specific topics in iOS. It is structured in a way that guides beginners and intermediate-level programmers through the information they need to understand the topic, while also allowing advanced readers to skip to exactly what they are looking for. That being said, this text is written with a few assumptions in mind.

  • The reader has a solid understanding of core programming concepts (object-oriented programming, pointers, functions)
  • The reader has a working knowledge of the basics of iOS development (using XCode, Interface Builder, and Cocoa Touch libraries)
  • The reader has programmed in Swift or Objective-C before.

As Swift and XCode are ever evolving tools, the first two chapters of this book cover IDE and syntax basics. Our goal with these chapters is to help developers who are still transitioning to Swift from Objective-C and those who have not yet had a chance to review Apple’s dense API update documents.

For expanded coverage of the Swift programming language, iOS programming, and XCode, we recommend the texts listed in Table 1, also available from Apress.

Table 1. Recommended References

Topic

Title and Author

Introductory iOS Development

Beginning iPhone Development with Swift by David Mark, Jack Nutting, Kim Topley, Fredrik Olsson, and Jeff LaMarche (Apress, 2014)

Using XCode and the Debugger

Beginning XCode: Swift Edition by Matthew Knott (Apress, 2014)

Intermediate iOS Development

Learn iOS8 App Development by James Bucanek (Apress, 2014)

Swift Syntax

Swift for Absolute Beginners by Gary Bennett and Brad Lees (Apress, 2014)

The most up-to-date reference for iOS programming is Apple’s official iOS Developer Library, which you can access from the “Documentation and API Reference” option in Xcode’s “Window” menu (shown in Figure 1) or online at https://developer.apple.com/library/ios/navigation/).

9781484211953_FM-01.jpg

Figure 1. Accessing the iOS Developer Library from XCode

Caution  Always use the latest version of the iOS Developer Library as your API reference. Apple often makes major deprecations and parameter changes, even in minor point version updates. The best way to stay up-to-date is by staying on the latest XCode version or accessing the iOS Developer Library web site.

What Do I Need to Get Started?

This book is designed around the workflow of developing an application on your computer and testing it on a physical device, which is potentially paired with a hardware accessory. The projects in this book depend on APIs which are not available in the iOS simulator. Apple’s requirements for developing and testing iOS applications with a physical device are the same as those required to submit an application to the App Store:

  • An Intel-based Mac running OS X Yosemite (10.10) or later
  • XCode 7 or later
  • An iPhone or iPad capable of running iOS9.1 or greater (iPhone 5 or greater, iPad2/iPad mini or greater)
  • A valid Apple ID to register for free, device-based testing of your apps

Starting in summer 2015, Apple removed the requirement of having a paid iOS Developer Program membership to test your apps on an iOS device. A paid membership is still required to submit your apps to the App Store, use TestFlight for beta testing, and debug Apple server-based APIs, such as Apple Pay. You can sign up for an Apple Developer Program membership by going to the Apple Developer Programs web site (https://developer.apple.com/programs/) and selecting the Enroll button, as shown in Figure 2. Upon receipt of your fees in the Apple Store, the Apple ID you selected will be available for use in the Apple Developer Program.

9781484211953_FM-02.jpg

Figure 2. Signing up for a paid Apple Developer Program account

Note  If you are signing up for a corporate developer account, you will need to provide Apple with additional information to identify your entity, such as a Dunn & Bradstreet number. It will take extra time to process your account.

The projects in this book are designed to be “universal,” meaning they can run on iPhone or iPad. The user interfaces are designed primarily for the iPhone, but they will be scaled up and work the same way on the iPad, as shown in Figure 3 for this chapter’s example.

9781484211953_FM-03.jpg

Figure 3. Sample project user interface on iPhone vs iPad

The Health section of this book requires you to have an iPhone 5S or greater, in order to use the Core Motion framework. iPads and older iPhones do not have the M-series motion co-processor chip which Core Motion provides access to. This chip includes an advanced pedometer and gyroscope, which allow motion tracking at less than one-tenth of the power consumption of the GPS chip (according to Apple.) For the FitBit chapter, you need a free account on Fitbit.com to generate sample data to test with, but you do not need a FitBit hardware device. You can manually enter in steps, caloric intake, and weight from the FitBit web site.

The first two chapters of the Apple Watch section can be implemented using the Apple Watch simulator; however, the last chapter takes advantage of Core Location features that are not supported by the simulator. The Apple Watch is still a hardware platform that is being optimized, so it is a good idea to use an Apple Watch to test your applications for realistic performance data.

The Bluetooth section requires you to have at least two iOS devices. The first chapter teaches you how to set up a direct link between two devices over Bluetooth. To keep the narrative focused, you will learn how to configure an iOS device for the two core roles of Bluetooth: central manager and peripheral. Having two devices allows you to test quickly and reliably. For the iBeacon chapter, you will learn how to configure an iPhone as an iBeacon, but you can also use a hardware beacon for testing (they are available as USB dongles at most electronics web sites for about $20-$30).

The “Internet of Secure Things” chapter (authentication and payment systems) requires you to have an iPhone 5S or later for the Touch ID chapter and an iPhone 6 or later for the Apple Pay chapter. The Touch ID sensor (identified by a metallic ring around the home button) is available on every iPhone since the 5S and every iPad since the iPad Mini3 and iPad Air. It cannot be emulated in software. Similarly, Apple Pay requires an NFC sensor and additional authentication chip that are only available in the iPhone 6/6 Plus or later, and iPad Mini 3/iPad Air 2 or later.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.164.143