Chapter 15. Accessing the Accelerometer


  • How to obtain accelerometer data from your iPhone

  • How to detect shakes to your device

iPhone's built-in accelerometer enables your program to detect the orientation of the device and adapt the content to suit the new orientation. For example, when you rotate your device sideways, the Safari Web browser automatically switches the screen to landscape mode to provide a wider viewing space.

In this chapter, you learn how to access the accelerometer and use the Shake API to detect shakes to your iPhone.

Using the Accelerometer

The accelerometer in iPhone measures the acceleration of the device relative to freefall. A value of 1 indicates that the device is experiencing 1 g of force exerted on it (1 g of force being the gravitational pull of the earth, which your device experiences when it is stationary). The accelerometer measures the acceleration of the device in three different axes: x, y and z. Figure 15-1 shows the different axes measured by the accelerometer.

Figure 15-1

Figure 15.1. Figure 15-1

Table 15-1 shows example readings of the three axes when the device is in the various positions. Bear in mind that you won't see the exact same values as these, because they are always fluctuating due to the accelerometer's sensitivity.

Table 15.1. Example Readings of the X, Y, and Z Axes





Vertical upright position




Get Beginning iOS 4 Application Development now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.