O'Reilly logo

Programming iOS 4 by Matt Neuburg

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

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 ...

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