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, orientation, and motion using its Wi-Fi and cellular networking, GPS, and 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 orientation using its accelerometer is provided through the UIEvent and UIAccelerometer classes. In iOS 4, this information is supplemented by the device’s gyroscope and is accessed through the Core Motion framework; you’ll link to CoreMotion.framework and import <CoreMotion/CoreMotion.h>.

Not all devices have all of this hardware. As of this writing, only an an iPad2, 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.

Location

Core Location provides facilities for the device to determine and report its location (location services). Even a device without GPS or cellular capabilities might be able to do this, by scanning for nearby Wi-Fi devices and comparing these against an online database. Core Location will automatically use whatever facilities the device does have; all you have to do is ask for the device’s location.

Asking a map view (MKMapView, Chapter 34) to ...

Get Programming iOS 4 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.