10.10. Sensors and Device Information

The iPhone includes many sensors, such as the accelerometer and proximity sensor. Other sensors, such as the orientation sensor, are used internally to automatically manage screen rotation, but may be of value for custom applications. In addition to sensors, the UIDevice class also allows you to read certain operating system-level information, such as the device's model, software version, and unique identifier.

10.10.1. Reading the Orientation

While most view controller classes do the work of changing the device's user interface, custom applications might choose to handle their own orientation changes. The orientation sensor can be read through the UIDevice class, and provides a set of simple enumerated values to identify the orientation.

To read the orientation, obtain an instance of the current UIDevice object:

UIDevice *device = [ UIDevice currentDevice ];

Access the orientation from the object's orientation property:

UIDeviceOrientation = device.orientation;

You may set the following values as the orientation.


Catchall for errors or hardware failures.


Oriented upright vertically in portrait mode.


Oriented upside-down vertically in portrait mode.


Device is rotated counter-clockwise in landscape mode.


Device is rotated clockwise in landscape mode.


Device is lying flat, face up; such as on a table.


Device is lying flat, face down; such as on a table.

10.10.2. Reading Device Information

In addition to the device's orientation, you can read other information from the UIDevice instance. To read these, first obtain an instance of the current device:

UIDevice *device = [ UIDevice currentDevice ];

You can subsequently read the following properties. Each provides an NSString object to describe the device.

Property Description
name Name assigned to the iPhone by its owner
model Device model; "iPhone," "iPod Touch," etc.
localizationModel The localized version of the model
systemName OS name; "iPhone OS"
systemVersion The operating system version
uniqueIdentifier Unique identifier of the device

10.10.3. Reading the Accelerometer

The orientation API gets its information from a small accelerometer built into the iPhone. This tiny piece of hardware reports the raw x-y-z position of the device. While reading the orientation can give you a general idea of how the device is being held, reading the accelerometer allows you to sense the slightest movement of the device. This is frequently used in games, where the user can move the device to steer a character or shake it to roll dice or erase a drawing pad.

Because the iPhone's accelerometer doesn't include a gyroscope, it can't provide information about speed, or as much detail about the state of the device as, say, a Nintendo Wii controller. It has proven useful, however, for simple applications such as simple games, bobble heads, and drawing programs.



Remember, not all of your customers will appreciate the iPhone's accelerometer. Adding an option to enable multi-touch controls can help to increase your potential audience.

To read the accelerometer, obtain the shared instance of the UIAccelerometer object, which is available to your application:

UIAccelerometer *accelerometer = [ UIAccelerometer sharedInstance ];

You can now access the x-y-z position of the accelerometer by accessing their respective properties, as shown below:

UIAccelerationValue x = accelerometer.x;
UIAccelerationValue y = accelerometer.y;
UIAccelerationValue z = accelerometer.z;

The UIAccelerationValue data type is defined as a double floating point. Tracking movement

To track movement of the accelerometer, you'll want your application to be notified whenever the device accelerates. To do this, assign an object as the accelerometer's delegate:

accelerometer.delegate = self;

Your delegate class will then be notified whenever the device accelerates:

- (void)accelerometer:(UIAccelerometer *)accelerometer
 didAccelerate:(UIAcceleration *)acceleration {
    /* Additional code to handle acceleration */

Finally, you can set the accelerometer's updateInterval property with an NSTimeInā terval to set the time interval at which you'd like to receive updates from the accelerometer:

NSTimeInterval timeInterval = 0.5;
accelerometer.updateInterval = timeInterval;

Don't set the update interval too low or your application might get bogged down with delegate calls. Apple internally specifies a minimum value, but this minimum does not appear to be published.

10.10.4. Proximity Sensor

If an application is performing a task that would normally require the user to hold the device to his face, the proximity sensor should be activated to cause the iPhone to shut its display off.

To activate the proximity sensor, obtain a shared instance of your UIApplication object and access its proximitySensingEnabled property:

UIApplication *myApp = [ UIApplication sharedApplication ];
myApp.proximitySensingEnabled = YES;

When active, the proximity sensor will automatically shut down the screen when it senses a face nearby. There is presently no sanctioned delegate method or other SDK-friendly means of notifying the application when this occurs. Apple must have thought some of their developers would do creepy things with it.


Be sure to disable the proximity sensor when the device should no longer be held to the user's face.

10.10.5. Further Study

  • Check out the UIDevice.h and UIAccelerometer.h prototypes. You'll find these deep within /Developer/Platforms/iPhoneOS.platform, inside the UI Kit framework's Headers directory.