Shannon Appelcline

Subscribe to Shannon Appelcline: eMailAlertsEmail Alerts
Get Shannon Appelcline: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn

Related Topics: iPhone Developer

iPhone Developer: Article

Book Excerpt: iPhone in Action

Using the iPhone's accelerometers

This article is based on iPhone in Action, to be published January 2009. It is being reproduced here by permission from Manning Publications. Manning early access books and ebooks are sold exclusively through Manning. Visit the book's page for more information.

The iPhone's accelerometers can provide access to a variety of information about where an iPhone exists in space. By measuring gravity, you can easily discover an iPhone's precise orientation. By measuring movement, you can see how an iPhone is being guided through space. Further, you can build more complex movements into three-dimensional gestures, such as the shake.

Although we usually think about the iPhone's touch screen in reference to input, the accelerometers provide another method that could allow users to make simple adjustments to a program. We can imagine game controls and painting programs both built entirely around the accelerometers.

The Accelerometer and Orientation
The easiest use of the accelerometers is to determine the iPhone's current orientation. The view controller's interfaceOrientation property can be used to determine orientation; however, you can also access that information through the UIDevice object. It can provide more information, as well as real-time access, that isn't available through the view controller.

There are two ways to access the UIDevice information: through properties and through a notification.

The Orientation Property
The easier way to access the UIDevice is to look at its orientation property. You must first access the UIDevice, which you can do by calling a special UIDevice class method, pretty much the same way you access the UIApplication object:

UIDevice *thisDevice = [UIDevice currentDevice];

Once you've done this, look at the orientation property. It will return a constant drawn from UIDeviceOrientation. This looks exactly like the results you can get from a view controller's orientation property except there are two additional values, as shown in Table 1.




iPhone is vertical, right side up


iPhone is vertical, upside down


iPhone is horizontal, tilted left


iPhone is horizontal, tilted right


iPhone is lying on its back


iPhone is lying on its screen

Table 1: UIDeviceOrientation lists six values for a device's orientation

These additional values show off the first reason that you might want to access the UIDevice object rather than examining orientation using a view controller.

The Orientation Notification
The UIDevice class can also give instant access to an orientation change when it occurs. This is done through a notification and is detailed in Listing 1.

As shown, this is a two-step process. First alert the iPhone that you're ready to start listening for notification that the iPhone has changed its orientation (#1). This is one of a pair of UIDevice methods, the other of which is endGeneratingDeviceOrientationNotifactions. Generally you should only leave notifications on when you actually need them, as they take up CPU cycles and thus increase your power consumption.

Second, register to receive the UIDeviceOrientationDidChangeNotification messages (#2), our first real-live example of the notification methods. Thereafter, whenever an orientation change notification occurs, your deviceDidRotate: method will be called. Note that you won't actually receive notification of what the new orientation is, simply that the change happened. For more, you'll have to go out and query the orientation property, as normal.

We've now seen the two ways in which an iPhone's orientation can be tracked with the UIDevice object, providing more information and more rapid notification than we received when using the view controller. However, that only touches the surface of what you can do with the iPhone's accelerometers. It's probably the raw data about changes in three-dimensional space that you really want to access.

More Stories By Christopher Allen

Christopher Allen is one of the leaders of the iPhone developer community. He is the host of, which is the largest independent community of iPhone-based web developers, and manages its mailing list. He is also one of the founders of iPhoneDevCamp and oversees its Hackathon, and is co-author of iPhone in Action:Introduction to Web and SDK Development. Christopher is a longtime technologist, and is also a leader in social software and was one of the authors of TLS, the next-generation SSL protocol.

More Stories By Shannon Appelcline

Shannon Appelcline is a writer and technologist. He was a participant in Charles River Media's Massively Multiplayer Game Development 2. He has also been published by Chaosium Inc., Issaries Inc., Jones Publishing, Partizan Press, White Wolf Publishing, and Wizards of the Coast. In 2007 he wrote over 350,000 words for professional publication, including books for Mongoose Publishing and Moon Design Publications. He has also written fiction published by Green Knight Publishing and comic books published by Skotos Tech.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.