O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Program the Internet of Things with Swift for iOS

Book Description

Program the Internet of Things with Swift and iOS is a detailed tutorial that will teach you how to build apps using Apple’s native APIs for the Internet of Things, including the Apple Watch, HomeKit, and Apple Pay. This is the second book by Ahmed Bakir (author of Beginning iOS Media App Development) and his team at devAtelier LLC, who have been involved in developing over 20 mobile projects.

Written like a code review, this book presents a detailed ""how"" and ""why"" for each topic, explaining Apple-specific design patterns as they come up and pulling lessons from other popular apps. To help you getting up and running quickly, each chapter is framed within a working project, allowing you to use the sample code directly in your apps.

The Internet of Things is not limited to Apple devices alone, so this book also explains how to interface with popular third-party hardware devices, such as the Fitbit and Raspberry Pi, and generic interfaces, like Restful API’s and HTTPS. The Internet of Things is waiting — be a part of it!

Table of Contents

  1. Cover
  2. Title
  3. Copyright
  4. Contents at a Glance
  5. Contents
  6. About the Authors
  7. About the Technical Reviewer
  8. Introduction
  9. Chapter 1: Building Your First Internet of Things App
    1. Ahmed Bakir
    2. Setting up the Project
    3. Building the User Interface
      1. Creating the Table View Controller
      2. Creating the Map View Controller
    4. Requesting Location Permission
    5. Accessing the User’s Location
    6. Displaying the User’s Location
      1. Populating the Table View
      2. Populating the Map
    7. Summary
  10. Chapter 2: Getting Started with Swift
    1. Ahmed Bakir
    2. Why Use Swift?
    3. Basic Swift Syntax
      1. Calling Methods (Hello World)
      2. Defining Variables
      3. Compound Data Types
      4. Conditional Logic
      5. Enumerated Types
      6. Loops
    4. Object-Oriented Programming in Swift
      1. Building a Class
      2. Protocols
      3. Method Signatures
      4. Accessing Properties and Methods
      5. Instantiating Objects
      6. Strings
      7. Formatting Strings
      8. Collections
      9. Casting
    5. Swift-Specific Language Features
      1. Optionals
      2. Try-Catch Blocks
    6. Mixing Objective-C and Swift in Projects
      1. Calling Swift from Objective-C
    7. Summary
  11. Chapter 3: Accessing Health Information Using HealthKit
    1. Introduction
    2. Getting Started
      1. Setting Up the User Interface
      2. Setting Up the Project for HealthKit
    3. Prompting the User for HealthKit Permission
      1. Retrieving Data from HealthKit
      2. Displaying Results in a Table View
      3. Fetching Background Updates
    4. Summary
  12. Chapter 4: Using Core Motion to Save Motion Data
    1. Introduction
    2. Using Core Motion to Access Motion Hardware
      1. Requesting User Permission for Motion Activity
    3. Querying for Step Count
      1. Detecting Live Updates to Step Count
      2. Detecting Activity Type
    4. Saving Data to HealthKit
    5. Summary
  13. Chapter 5: Integrating Third-Party Fitness Trackers and Data Using the Fitbit API
    1. Gheorghe Chesler
    2. Introduction to the Fitbit API
      1. The RESTful API
      2. Fitbit RESTful API Implementation Details
      3. Setting Up a Local Playground with Apache
      4. The OAuth1.0a Authentication Model
      5. The Fitbit OAuth Implementation
      6. Fitbit API Call Rate Limits
      7. Making async Calls
      8. Using callbacks as Parameters
    3. Setting up a Fitbit-compatible iOS Project
      1. The View Controller
      2. The Logger Library
      3. Setting up a Basic Set of Crypto Functions
      4. The API Client Library
      5. The OAuth Library
      6. Testing What We Have so Far
    4. Making requests to the Fitbit API
      1. Retrieving the User Profile
      2. Retrieving and Setting Data in the API
      3. OAuth versions: Working in both worlds
    5. Summary
  14. Chapter 6: Building Your First watchOS App
    1. Introduction
    2. watchOS Appsvs. iOS Apps
    3. Setting Up Your Project
      1. Debugging your watchOS App
    4. Adding a Table to your watchOS App
      1. Defining the Table
      2. Fetching Data from your iOS App
    5. Building a Detail Page with a Custom Layout
      1. Presenting the Detail Interface Controller
    6. Summary
  15. Chapter 7: Building an Interactive watchOS App
    1. Introduction
    2. Using Force Touch to Present Menus
      1. Resetting the Location List
      2. Presenting a Detail View Controller
      3. Simulating Force Touch
    3. Adding Buttons to an Interface Controller
    4. Passing Information Between Interface Controllers
      1. Using a Delegate to Pass Information on Dismissal
    5. How to Add Notes Using Text Input
      1. Sending Data Back to the Parent iOS App
    6. Summary
  16. Chapter 8: Building a Stand-Alone watchOS App
    1. Using Core Location to Request Current Location
      1. Reverse Geocoding an Address
    2. Using NSTimer to Create Reminders
    3. Making Network Calls from Your watchOS App
      1. Handling a JSON Response
    4. Summary
  17. Chapter 9: Connecting to a Bluetooth LE Device
    1. Manny de la Torriente
    2. Introduction to the Apple Bluetooth stack
      1. Key Terms and Concepts
      2. Core Bluetooth Objects
    3. Building Your First Bluetooth LE Application
    4. Backlog
      1. Base Application and Home Scene
      2. Central Role Scene
      3. Peripheral Role Scene
      4. Editable Text
    5. Setting Up the Project
    6. Building the Interface
    7. Using a Central Manager
    8. Connecting to a Bluetooth LE Device in Your App
      1. Building the Interface
      2. Keeping Things Clean with Delegation
      3. Scanning for Peripherals
      4. Discover and Connect
      5. Explore Services and Characteristics
      6. Subscribe and Receive Data
    9. Peripheral Role
      1. Building the Interface
      2. Delegate Setup
      3. Setting up a Service
      4. Advertising Services
      5. Sending Data
    10. Enabling Your App for Background Communication
    11. Bluetooth Best Practices
      1. Central Role Devices
      2. Peripheral Role Devices
    12. Summary
  18. Chapter 10: Building Location Awareness with iBeacons
    1. Manny de la Torriente
    2. Introduction to iBeacons
      1. iBeaconAdvertisement
      2. iBeaconAccuracy
      3. Privacy
      4. Region Monitoring
    3. Ranging
    4. Building the iBeaconApp Application
      1. Creating the Project
      2. Setting Background Capabilities
    5. Building the Home Scene
      1. Setting Up UI Elements
      2. Creating an Outlet Connection
      3. Setting up Constraints
      4. Creating a Custom Button
    6. Detecting Bluetooth State
    7. Building the Region Monitor Scene
      1. The RegionMonitor Class
      2. Using the Delegation Pattern
      3. Creating the RegionMonitor Class
      4. Delegate Methods
      5. RegionMonitor Methods
      6. Authorization and Requesting Permission
      7. CLLocationManagerDelegate Methods
    8. Building the iBeacon Scene
      1. The BeaconTransmitter Class
      2. Defining the BeaconTransmitterDelegate Protocol
    9. Summary
  19. Chapter 11: Home Automation Using HomeKit
    1. Manny de la Torriente
    2. Introduction to HomeKit Concepts
    3. HomeKit Delegation Methods
    4. Building a HomeKit Application
      1. Requirements
      2. HomeKit Accessory Simulator
    5. Creating the Project
      1. Enabling HomeKit
      2. Building the Homes Interface
      3. Implementing the Home Manager Delegate Methods
      4. Adding a New Home to the Home Manager
      5. Removing an Accessory from a Home
    6. Transitioning to the Services Scene
    7. Running the Application
      1. Adding Accessories
    8. Summary
  20. Chapter 12: Building an App That Interacts with a Raspberry Pi
    1. Gheorghe Chesler
    2. About Your Raspberry Pi
    3. Control Interfaces on your Raspberry Pi
    4. Setting up your Raspberry Pi
      1. Choosing the Scripting Language
      2. Configuring I2C
      3. Configuring GPIO
      4. Install PyGlow
    5. Providing an API to Control your Device
      1. Install Flask
    6. Setting up an iOS Project for Our App
      1. Allowing Outgoing HTTP Calls
      2. The View Controller
      3. The Logger Library
    7. Summary
  21. Chapter 13: Using Keychain Services to Secure Data
    1. Hardware Security on iOS Devices
    2. Securing the File Data
    3. The Apple Keychain
      1. The Apple Keychain Services
      2. Components of a Keychain Item
      3. Implementing Keychain Services for Storing Passwords
      4. Retrieving Data from Keychain Services
      5. Invalidating Keychain Service Records
      6. Setting Up an Application to Test Keychain Services
      7. The View Controller
    4. Summary
  22. Chapter 14: Using Touch ID for Local Authentication
    1. Manny de la Torriente
    2. Introduction to Touch ID
      1. LocalAuthentication Use Cases
      2. Building a Touch ID Application
    3. Creating the Project
    4. Building the Interface
    5. Implementing the UITableView Methods
    6. Integrating Touch ID for Fingerprint Authentication
      1. Evaluating Authentication Policies
      2. Touch ID Authentication without Keychain
      3. User-Defined Fallback for Authentication
      4. Run the Application
    7. Things to Remember
    8. Summary
  23. Chapter 15: Using Apple Pay to Accept Payments
    1. Gheorghe Chesler
    2. Apple Pay vs. Alternative Payment Systems
      1. Apple Pay Prerequisites
      2. Using Apple Pay to accept payments
      3. Configuring your Environment for Apple Pay
      4. Implementing Apple Pay payments with Stripe
      5. The View Controller code
    3. Summary
  24. Index