Preface - Barcodes with iOS: Bringing together the digital and physical worlds (2015)

Barcodes with iOS: Bringing together the digital and physical worlds (2015)


When Apple released the first beta of iOS 7 at WWDC 2013, I scoured through the API changes looking for anything out of the ordinary. That’s when I noticed the unexpected addition of new APIs pertaining to barcodes.

At that time, barcodes were little more than visual noise to me, a necessity of modern commerce but of no value to me as a consumer or app developer. Why would Apple devote precious resources to implementing functionality for that?

Several third-party libraries for barcode scanning were available at that time. Some were commercial offerings too expensive for casual use. Others were open source projects requiring a great deal of work to understand or implement in your own apps. By adding support for barcodes within the iOS SDK, Apple made the technology accessible to all developers equally. Apple was sending a message: barcodes are important to us.

This paradigm shift inspired me to learn all I could about barcode technologies. I began to research the barcode types supported by iOS and their capabilities and limitations, and all the new related iOS APIs.

A mere month after WWDC 2013, I was contacted by Manning. They’d found me via my blog ( and inquired if I would be interested in writing an iOS book for them. They could not have contacted me at a more perfect moment! I was willing, able, and inspired to write, for more than a year, the book you’re now holding.

June 2014 marked the 40-year anniversary of the first barcode being scanned at a point of sale. In other words, barcodes are a nearly ubiquitous, mature technology. The UPC you’ll find on all products sold in your supermarket was just the beginning. Just look at any Apple product box. You’ll find several barcodes on the stickers offering additional information such as the device’s serial number.

Since October 2013, all iPhones can be used to scan barcodes. Together with always-on mobile internet and built-in device sensors, this enables a new breed of product-centric apps that weren’t feasible before.

After reading this book, you’ll be able to build the exciting new apps that are bringing together the digital and physical worlds.

About this Book

This book is intended for intermediate-level iOS developers who know their way around Xcode and have built a few apps already. This allows me to provide instructions in a terse, tutorial-like style, as readers of my blog,, have come to appreciate. By focusing on a more experienced audience, I don’t have to waste your time with iOS development basics that are well covered in other books.


The topic of barcodes serves as the common thread running through the chapters of this book, but you can dive into specific chapters to learn about particular technologies. Here’s a quick overview of what you will find in each of the chapters.

Chapter 1 introduces you to the barcode types that are natively supported starting with iOS 7. You’ll become a barcode guru and be able to hold your own in any conversation about barcodes, their promises, and their limitations. If you like a good story, flip to appendix A, which recounts the curious history of the mother of all barcodes: the UPC.

Chapter 2 gives you a solid introduction to AV Foundation media capture. You’ll become familiar with the components of this framework, which we’ll use to build a camera app.

Chapter 3 introduces you to the metadata detectors for scanning barcodes, building on the camera preview from chapter 2. At this point, you’ll have a reusable barcode scanner that you can employ in all your apps.

Chapter 4 deals with Apple’s main reason for pushing forward with barcode support in iOS: Passbook. You’ll learn how to generate Passbook tickets with Ruby and how to validate them in an iOS app without the need for server infrastructure.

Chapter 5 looks at how you can generate your own barcodes for display on devices and how you can print them to physical media via AirPrint. You’ll learn about the use of Core Image for generating QR Codes and BarCodeKit for all kinds of 1D barcodes.

Chapter 6 dives into retrieving metadata for scanned barcodes, in particular how to use NSURLSession for creating a web service wrapper. The second half of this chapter is about creating a custom NSURLProtocol and how to stub network requests for unit testing without the need to call an actual server over the internet.

Chapter 7 then rounds out the book by adding contextual information about the user who’s scanning barcodes. Core Location and iBeacons let you magically adapt your app’s UI to your user’s needs.

Three appendixes provide additional background and other useful information.

Code conventions and downloads

All source code in listings or in text is in a fixed-width font like this to separate it from ordinary text. Code annotations accompany many of the listings, highlighting important concepts. In some cases, numbered bullets link to explanations that follow the listing.

Source code for all working examples in this book is available for download from the publisher’s website at