O'Reilly logo

Basic Sensors in iOS by Alasdair Allan

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


Over the last few years the new generation of smart phones, such as Apple’s iPhone, has finally started to live up to their name and have become the primary interface device for geographically tagged data. However not only do these devices know where they are, they can tell you how they’re being held, they are sufficiently powerful to overlay data layers on the camera view, and record and interpret audio data, and they can do all this in real time. These are not just smart phones, these are computers that just happen to be able to make phone calls.

This book should provide a solid introduction to using the hardware features in the iPhone, iPod touch, and iPad.

Who Should Read This Book?

This book provides an introduction to the hot topic of location-enabled sensors on the iPhone. If you are a programmer who has had some experience with the iPhone before, this book will help you push your knowledge further. If you are an experienced Mac programmer, already familiar with Objective-C as a language, this book will give you an introduction to the hardware specific parts of iPhone programming.

What You Should Already Know?

The book assumes some previous experience with the Objective-C language. Additionally some familiarity with the iPhone platform would be helpful. If you’re new to the iPhone platform you may be interested in Learning iPhone Programming, also by Alasdair Allan (O’Reilly).

What Will You Learn?

This book will guide you through guide you through developing applications for the iPhone platform that make use of the onboard sensors: the three-axis accelerometer, the magnetometer (digital compass), the gyroscope, the camera and the global positioning system

What’s In This Book?

Chapter 1, The Hardware

This chapter summarizes the available sensors on the iPhone and iPad platforms and how they have, or could be, used in applications. It talks about the differences between the hardware platforms.

Chapter 2, Using the Camera

Walkthrough of how to use the iPhone’s camera for still and video. How to create video thumbnails and customise video.

Chapter 3, Using Audio

Walkthrough of how to playback iPod media, as well as how to play and record audio on your device.

Chapter 4, Using the Accelerometer

Walkthrough of how to use the accelerometer, discussion of what is implied with respect to the orientation of the device by the raw readings.

Chapter 5, Using the Magnetometer

Walkthrough of how to use the magnetometer, discussion of combining the magnetometer and accelerometer to get the yaw, pitch and roll of the device.

Chapter 6, Using Core Motion

This paragraph discusses the new Core Motion framework; this new framework allows your application to receive motion data from both the accelerometer and (on the latest generation of devices) the gyroscope.

Chapter 7, Going Further

Provides a collection of pointers to more advanced material on the topics we covered in the book, and material covering some of those topics that we didn’t manage to talk about in this book.

Conventions Used in This Book

The following typographical conventions are used in this book:


Indicates new terms, URLs, email addresses, filenames, and file extensions.

Constant width

Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.

Constant width bold

Shows commands or other text that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values or by values determined by context.


This icon signifies a tip, suggestion, or general note.


This icon signifies a warning or caution.

Using Code Examples

This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Basic Sensors in iOS, by Alasdair Allan. Copyright 2011 O’Reilly Media, Inc., 978-1-4493-0846-9.”

If you feel your use of code examples falls outside fair use or the permission given here, feel free to contact us at .


A lot of the examples won’t work completely in the simulator, so you’ll need to deploy them to your device to test the code.

Safari® Books Online


Safari Books Online is an on-demand digital library that lets you easily search over 7,500 technology and creative reference books and videos to find the answers you need quickly.

With a subscription, you can read any page and watch any video from our library online. Read books on your cell phone and mobile devices. Access new titles before they are available for print, and get exclusive access to manuscripts in development and post feedback for the authors. Copy and paste code samples, organize your favorites, download chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other time-saving features.

O’Reilly Media has uploaded this book to the Safari Books Online service. To have full digital access to this book and others on similar topics from O’Reilly and other publishers, sign up for free at http://my.safaribooksonline.com.

How to Contact Us

Please address comments and questions concerning this book to the publisher:

O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:


Supplementary materials are also available at:


To comment or ask technical questions about this book, send email to:

For more information about our books, courses, conferences, and news, see our website at http://www.oreilly.com.

Find us on Facebook: http://facebook.com/oreilly

Follow us on Twitter: http://twitter.com/oreillymedia

Watch us on YouTube: http://www.youtube.com/oreillymedia


Everyone has one book in them. This is my second, or depending how you want to look at it, my Platform 9¾, since this book, along with the other three forthcoming short books on iOS and sensor technology, will form the bulk of Programming iOS4 Sensors, which would probably be classed by most as my second real book for O’Reilly.

At which point, I’d like to thank my editor at O’Reilly, Brian Jepson, for holding my hand just one more time. As ever his hard work made my hard work much better than it otherwise would have been. I also very much want to thank my wife Gemma Hobson for her continued support and encouragement. Those small, and sometimes larger, sacrifices an author’s spouse routinely has to make don’t get any less inconvenient the second time around. I’m not sure why she let me write another, perhaps because I claimed to enjoy writing the first one so much. Thank you Gemma. Finally to my son Alex, still too young to read what his daddy has written, hopefully this volume will keep you in books to chew on just a little longer.

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

Start Free Trial

No credit card required