Chapter 35. Sensors

A device may contain hardware for sensing the world around itself — where it is located, how it is oriented, how it is moving.

Information about the device’s current location and how that location is changing over time, using its Wi-Fi, cellular networking, and GPS capabilities, along with information about the device’s orientation relative to north, using its magnetometer, is provided through the Core Location framework. You’ll link to CoreLocation.framework and import <CoreLocation/CoreLocation.h>.

Information about the device’s change in speed and attitude using its accelerometer is provided through the UIEvent and UIAccelerometer classes. Starting iOS 4, this information is supplemented by the device’s gyroscope, if it has one, and is accessed through the Core Motion framework; you’ll link to CoreMotion.framework and import <CoreMotion/CoreMotion.h>. In iOS 5, UIAccelerometer, while not formally deprecated, is slated for deprecation; Apple would like you to use Core Motion exclusively if possible.

One of the major challenges associated with writing code that takes advantage of the sensors is that not all devices have all of this hardware: as of this writing, only an iPad 2, an iPhone 4, or a fourth-generation iPod touch has a gyroscope; an iPod touch has no magnetometer; a device with only Wi-Fi (no cellular networking) cannot detect cell towers and also lacks a built-in GPS; and so forth. If you don’t want to impose stringent restrictions on what devices your ...

Get Programming iOS 5, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.