Solutions & Examples for iOS Apps
1234567

Entirely rewritten for Apple’s Swift programming language, this updated cookbook helps you overcome the vexing issues you’re likely to face when creating apps for iOS devices.

You’ll find hundreds of new and revised recipes for using the iOS 8 SDK, including techniques for working with Health data and HomeKit accessories, enhancing and animating graphics, storing and protecting data, sending and receiving notifications, and managing files and folders among them. Each recipe includes sample code on GitHub that you can use right away.

  • Use CloudKit APIs to store information in the cloud with ease
  • Create custom keyboards and extensions
  • Access users’ health-related information with HealthKit
  • Interact with accessories inside the user’s home with HomeKit
  • Create vibrant and lifelike user interfaces with UIKit Dynamics
  • Use the Keychain to protect your app’s data
  • Develop location-aware and multitasking-aware apps
  • Work with iOS 8’s audio and video APIs
  • Use Event Kit UI to manage calendars, dates, and events
  • Take advantage of the accelerometer and the gyroscope
  • Get working examples for implementing gesture recognizers
  • Retrieve and manipulate contacts and groups from the Address Book
  • Determine a camera’s availability and access the Photo Library

Vandad Nahavandipoor

Vandad Nahavandipoor is currently an iOS programmer for a leading digital media distributor in London, United Kingdom. He has led an international team of more than 30 iOS developers in his previous job. Some of the projects he has lead include the NatWest and the RBS iOS apps running on millions of iPhones and iPads in the UK. Vandad received his B.Sc and M.Sc in Information Technology for E-Commerce from the University of Sussex in England.

Vandad's programming experience started when he first learned Basic on his father's Commodore 64. He then took this experience and applied it on his uncle's Intel 186 computer, running Basic on DOS. At this point, he found programming for personal computers exciting indeed and moved on to learn Object Pascal. This allowed him to learn Borland Delphi quite easily. He wrote a short 400 pages book on Borland Delphi and dedicated the book to Borland. From then, he picked up x86 Assembly programming and wrote a hobby 32-bit operating system named Vandior. It wasn't until late 2007 when iOS programming became his main focus.

Aside from programming, Vandad is a road cyclist, enjoying friendly competition in cyclosportive events in England and traveling long distance on his bicycle to other European countries every now and then. He also enjoys tinkering with his piano and playing electric guitar. Some of his electric guitar performances are available on YouTube.

  1. Chapter 1: The Basics

    1. 1.0. Introduction

    2. 1.1. Adding Blur Effects to Your Views

    3. 1.2. Presenting Temporary Information on the Screen with Popovers

    4. 1.3. Displaying Images with UIImageView

    5. 1.4. Displaying Static Text with UILabel

    6. 1.5. Adding Buttons to the User Interface with UIButton

    7. 1.6. Displaying Alerts and Action Sheets

    8. 1.7. Creating, Using, and Customizing Switches with UISwitch

    9. 1.8. Picking Values with the UIPickerView

    10. 1.9. Picking the Date and Time with UIDatePicker

    11. 1.10. Implementing Range Pickers with UISlider

    12. 1.11. Grouping Compact Options with UISegmentedControl

    13. 1.12. Presenting Sharing Options with UIActivityViewController

    14. 1.13. Presenting Custom Sharing Options with UIActivityViewController

    15. 1.14. Displaying an Image on a Navigation Bar

    16. 1.15. Adding Buttons to Navigation Bars Using UIBarButtonItem

    17. 1.16. Accepting User Text Input with UITextField

    18. 1.17. Displaying Long Lines of Text with UITextView

    19. 1.18. Creating Scrollable Content with UIScrollView

    20. 1.19. Loading Web Pages with WebKit

    21. 1.20. Loading Web Pages with UIWebView

    22. 1.21. Displaying Progress with UIProgressView

    23. 1.22. Creating a Provision Profile

  2. Chapter 2: Extensions

    1. 2.0. Introduction

    2. 2.1. Adding New Photo Editing Capabilities to the Photos App

    3. 2.2. Providing a Custom Sharing Extension to iOS

    4. 2.3. Building Custom Keyboards

    5. 2.4. Creating a Service Within Your App with Action Extensions

    6. 2.5. Adding Widgets to the Notification Center

  3. Chapter 3: Managing Health Data with HealthKit

    1. 3.0. Introduction

    2. 3.1. Setting Up Your App for HealthKit

    3. 3.2. Retrieving and Modifying the User’s Weight Information

    4. 3.3. Accessing and Modifying the User’s Height Information

    5. 3.4. Retrieving User Characteristics

    6. 3.5. Observing Changes to the User’s Health Information

    7. 3.6. Reading and Modifying the User’s Total Calories Burned

    8. 3.7. Converting Between Units

  4. Chapter 4: Managing Home Appliances with HomeKit

    1. 4.0. Introduction

    2. 4.1. Simulating HomeKit Accessories

    3. 4.2. Managing the User’s Home in HomeKit

    4. 4.3. Adding Rooms to the User’s Home

    5. 4.4. Specifying Zones in the User’s Home

    6. 4.5. Discovering and Managing HomeKit Enabled Accessories

    7. 4.6. Interacting with HomeKit Accessories

    8. 4.7. Grouping Services of HomeKit Accessories

  5. Chapter 5: Creating Dynamic and Interactive User Interfaces

    1. 5.0. Introduction

    2. 5.1. Adding Gravity to Your UI Components

    3. 5.2. Detecting and Reacting to Collisions Between UI Components

    4. 5.3. Animating Your UI Components with a Push

    5. 5.4. Attaching Multiple Dynamic Items to Each Other

    6. 5.5. Adding a Dynamic Snap Effect to Your UI Components

    7. 5.6. Assigning Characteristics to Your Dynamic Effects

  6. Chapter 6: Table and Collection Views

    1. 6.0. Introduction

    2. 6.1. Populating a Table View with Data

    3. 6.2. Enabling Swipe Deletion of Table View Cells

    4. 6.3. Constructing Headers and Footers in Table Views

    5. 6.4. Displaying a Refresh Control for Table Views

    6. 6.5. Providing Basic Content to a Collection View

    7. 6.6. Feeding Custom Cells to Collection Views Using .xib Files

    8. 6.7. Handling Events in Collection Views

    9. 6.8. Providing Header and Footer in a Collection View

    10. 6.9. Adding Custom Interactions to Collection Views

  7. Chapter 7: Concurrency and Multitasking

    1. 7.0. Introduction

    2. 7.1. Performing UI-Related Tasks

    3. 7.2. Performing Non-UI Related Tasks

    4. 7.3. Performing Tasks After a Delay

    5. 7.4. Performing a Task Only Once

    6. 7.5. Grouping Tasks Together

    7. 7.6. Creating Simple Concurrency with Operations

    8. 7.7. Creating Dependency Between Operations

    9. 7.8. Firing Periodic Tasks

    10. 7.9. Completing a Long-Running Task in the Background

    11. 7.10. Adding Background Fetch Capabilities to Your Apps

    12. 7.11. Playing Audio in the Background

    13. 7.12. Handling Location Changes in the Background

    14. 7.13. Handling Network Connections in the Background

  8. Chapter 8: Security

    1. 8.0. Introduction

    2. 8.1. Authenticating the User with Touch ID

    3. 8.2. Enabling Security and Protection for Your Apps

    4. 8.3. Storing Values in the Keychain

    5. 8.4. Finding Values in the Keychain

    6. 8.5. Updating Existing Values in the Keychain

    7. 8.6. Deleting Existing Values in the Keychain

    8. 8.7. Sharing Keychain Data Between Multiple Apps

    9. 8.8. Writing to and Reading Keychain Data from iCloud

    10. 8.9. Storing Files Securely in the App Sandbox

    11. 8.10. Securing Your User Interface

  9. Chapter 9: Core Location, iBeacon, and Maps

    1. 9.0. Introduction

    2. 9.1. Detecting Which Floor the User Is on in a Building

    3. 9.2. Defining and Processing iBeacons

    4. 9.3. Pinpointing the Location of a Device

    5. 9.4. Displaying Pins on a Map View

    6. 9.5. Displaying Custom Pins on a Map View

    7. 9.6. Searching on a Map View

    8. 9.7. Displaying Directions on the Map

    9. 9.8. Customizing the View of the Map with a Camera

  10. Chapter 10: Gesture Recognizers

    1. 10.0. Introduction

    2. 10.1. Detecting Swipe Gestures

    3. 10.2. Detecting Rotation Gestures

    4. 10.3. Detecting Panning and Dragging Gestures

    5. 10.4. Detecting Long Press Gestures

    6. 10.5. Detecting Tap Gestures

    7. 10.6. Detecting Pinch Gestures

    8. 10.7. Detecting Screen Edge Pan Gestures

  11. Chapter 11: Networking and Sharing

    1. 11.0. Introduction

    2. 11.1. Downloading Data Using NSURLSession

    3. 11.2. Downloading Data in the Background Using NSURLSession

    4. 11.3. Uploading Data Using NSURLSession

    5. 11.4. Downloading Asynchronously with NSURLConnection

    6. 11.5. Handling Timeouts in Asynchronous Connections

    7. 11.6. Downloading Synchronously with NSURLConnection

    8. 11.7. Customizing URL Requests

    9. 11.8. Sending HTTP Requests with NSURLConnection

    10. 11.9. Serializing and Deserializing JSON Objects

    11. 11.10. Integrating Social Sharing into Your Apps

  12. Chapter 12: Multimedia

    1. 12.0. Introduction

    2. 12.1. Playing Audio Files

    3. 12.2. Recording Audio

    4. 12.3. Playing Video Files

    5. 12.4. Capturing Thumbnails from Video Files

    6. 12.5. Accessing the Music Library

  13. Chapter 13: Address Book

    1. 13.0. Introduction

    2. 13.1. Retrieving a Person Entity with System UI

    3. 13.2. Retrieving a Property of a Person Entity with System UI

    4. 13.3. Requesting Access to the Address Book

    5. 13.4. Retrieving All the People in the Address Book

    6. 13.5. Retrieving Properties of Address Book Entries

    7. 13.6. Inserting a Person Entry into the Address Book

    8. 13.7. Inserting a Group Entry into the Address Book

    9. 13.8. Adding Persons to Groups

    10. 13.9. Searching the Address Book

    11. 13.10. Retrieving and Setting a Person’s Address Book Image

  14. Chapter 14: Files and Folder Management

    1. 14.0. Introduction

    2. 14.1. Finding the Paths of the Most Useful Folders on Disk

    3. 14.2. Writing to and Reading from Files

    4. 14.3. Creating Folders on Disk

    5. 14.4. Enumerating Files and Folders

    6. 14.5. Deleting Files and Folders

    7. 14.6. Saving Objects to Files

  15. Chapter 15: Camera and the Photo Library

    1. 15.0. Introduction

    2. 15.1. Detecting and Probing the Camera

    3. 15.2. Taking Photos with the Camera

    4. 15.3. Taking Videos with the Camera

    5. 15.4. Storing Photos in the Photo Library

    6. 15.5. Storing Videos in the Photo Library

    7. 15.6. Searching for and Retrieving Images and Videos

    8. 15.7. Reacting to Changes in Images and Videos

    9. 15.8. Editing Images and Videos Right on the Device

  16. Chapter 16: Notifications

    1. 16.0. Introduction

    2. 16.1. Sending Notifications

    3. 16.2. Listening for and Reacting to Notifications

    4. 16.3. Listening and Reacting to Keyboard Notifications

    5. 16.4. Scheduling Local Notifications

    6. 16.5. Listening for and Reacting to Local Notifications

    7. 16.6. Handling Local System Notifications

    8. 16.7. Setting Up Your App for Push Notifications

    9. 16.8. Delivering Push Notifications to Your App

    10. 16.9. Reacting to Push Notifications

  17. Chapter 17: Core Data

    1. 17.0. Introduction

    2. 17.1. Performing Batch Updates on Core Data

    3. 17.2. Writing to Core Data

    4. 17.3. Reading Data from Core Data

    5. 17.4. Deleting Data from Core Data

    6. 17.5. Sorting Data in Core Data

    7. 17.6. Boosting Data Access in Table Views

    8. 17.7. Implementing Relationships in Core Data

    9. 17.8. Fetching Data in the Background

    10. 17.9. Using Custom Data Types in Your Core Data Model

  18. Chapter 18: Dates, Calendars, and Events

    1. 18.0. Introduction

    2. 18.1. Constructing Date Objects

    3. 18.2. Retrieving Date Components

    4. 18.3. Requesting Permission to Access Calendars

    5. 18.4. Retrieving Calendar Groups on an iOS Device

    6. 18.5. Adding Events to Calendars

    7. 18.6. Accessing the Contents of Calendars

    8. 18.7. Removing Events from Calendars

    9. 18.8. Adding Recurring Events to Calendars

    10. 18.9. Retrieving the Attendees of an Event

    11. 18.10. Adding Alarms to Calendars

  19. Chapter 19: Graphics and Animations

    1. 19.0. Introduction

    2. 19.1. Drawing Text

    3. 19.2. Drawing Images

    4. 19.3. Constructing Resizable Images

    5. 19.4. Drawing Lines

    6. 19.5. Constructing Paths

    7. 19.6. Drawing Rectangles

    8. 19.7. Adding Shadows to Shapes

    9. 19.8. Drawing Gradients

    10. 19.9. Transforming Views

    11. 19.10. Animating Views

  20. Chapter 20: Core Motion

    1. 20.0. Introduction

    2. 20.1. Retrieving Altitude Data

    3. 20.2. Retrieving Pedometer Data

    4. 20.3. Detecting the Availability of an Accelerometer

    5. 20.4. Detecting the Availability of a Gyroscope

    6. 20.5. Retrieving Accelerometer Data

    7. 20.6. Detecting Shakes on an iOS Device

    8. 20.7. Retrieving Gyroscope Data

  21. Chapter 21: Cloud

    1. 21.0. Introduction

    2. 21.1. Setting Up Your App for CloudKit

    3. 21.2. Storing Data with CloudKit

    4. 21.3. Retrieving Data with CloudKit

    5. 21.4. Querying the Cloud with CloudKit

    6. 21.5. Observing Changes to Records in CloudKit

    7. 21.6. Retrieving User Information from CloudKit

    8. 21.7. Storing and Synchronizing Dictionaries in iCloud

    9. 21.8. Creating and Managing Files and Folders in iCloud

    10. 21.9. Searching for Files and Folders in iCloud