Writing an Accelerometer Application

Let’s go ahead and implement a simple application to illustrate how to approach the accelerometer. Open Xcode and start a new View-based application for the iPhone, and name the project “Accelerometer” when prompted for a filename.

Warning

The raw accelerometer data can also be accessed using the Core Motion framework, which was new in iOS 4.0. I talk about how to do this in Chapter 6. It is therefore possible, even likely, that the UIAccelerometer class discussed in this chapter my be deprecated in a future iOS release.

Click on the AccelerometerViewController.xib file to open it into Interface Builder. Since you want to both report the raw figures from the accelerometer and also display them using a progress bar, go ahead and drag and drop three UIProgressView controls from the Object Library into the View window. Then add two UILabel elements for each progress bar: one to hold the X, Y, or Z label and the other to hold the accelerometer measurements. After you do that, the view should look something a lot like Figure 4-2.

The Accelerometer application UI

Figure 4-2. The Accelerometer application UI

Go ahead and close the Utilities panel and click to open the Assistant Editor. Then Control-Click and drag from the three UIProgressView elements, and the three UILabel elements to the AcclerometerViewController.h header file. The header file should be displayed in the Assistant Editor on the right-hand side of the Xcode 4 interface (see Figure 4-3).

Connecting the UI elements to your code in Interface Builder

Figure 4-3. Connecting the UI elements to your code in Interface Builder

This will automatically create and declare three UILabel and three UIProgressView variables as IBOutlet objects. Since they aren’t going to be used outside the class, there isn’t much point in declaring them as class properties, which you’d do by Control-click and drag from the element to outside the curly brace. After doing this the code should look like this:

#import <UIKit/UIKit.h>

@interface AccelerometerViewController :  UIViewController  {

    IBOutlet UIProgressView *xBar;
    IBOutlet UIProgressView *yBar;
    IBOutlet UIProgressView *zBar;

    IBOutlet UILabel *xLabel;
    IBOutlet UILabel *yLabel;
    IBOutlet UILabel *zLabel;
}

@end

Close the Assistant Editor, return to the Standard Editor and click on the AccelerometerViewController.h interface file. Now go ahead and set up a UIAccelerometer instance. Also declare the class as a UIAccelerometerDelegate. Here’s how the should look when you are done:

#import <UIKit/UIKit.h>

@interface AccelerometerViewController :
  UIViewController <UIAccelerometerDelegate> { 1

    IBOutlet UILabel *xLabel;
    IBOutlet UILabel *yLabel;
    IBOutlet UILabel *zLabel;

    IBOutlet UIProgressView *xBar;
    IBOutlet UIProgressView *yBar;
    IBOutlet UIProgressView *zBar;

    UIAccelerometer *accelerometer;
}

@end
1

Here we declare that the class implements the UIAccelerometer delegate protocol.

Make sure you’ve saved your changes and click on the corresponding AccelerometerViewController.m implementation file to open it in the Xcode editor. You don’t actually have to do very much here, as Interface Builder handled most of the heavy lifting by adding code to properly handle the user interface elements. Here’s what the file should look like when you are done:

#import "AccelerometerViewController.h"

@implementation AccelerometerViewController

- (void)viewDidLoad {
    accelerometer = [UIAccelerometer sharedAccelerometer];1
    accelerometer.updateInterval = 0.1;2
    accelerometer.delegate = self;3
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload
{
    [xBar release];
    xBar = nil;
    [yBar release];
    yBar = nil;
    [zBar release];
    zBar = nil;
    [xLabel release];
    xLabel = nil;
    [yLabel release];
    yLabel = nil;
    [zLabel release];
    zLabel = nil;
    [super viewDidUnload];
}

- (void)dealloc {
    [xLabel release];
    [yLabel release];
    [zLabel release];
    [xBar release];
    [yBar release];
    [zBar release];

    accelerometer.delegate = nil;
    [accelerometer release];

    [super dealloc];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:
      (UIInterfaceOrientation)interfaceOrientation {
      
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark UIAccelerometerDelegate Methods

- (void)accelerometer:(UIAccelerometer *)meter
  didAccelerate:(UIAcceleration *)acceleration 4
{
    xLabel.text = [NSString stringWithFormat:@"%f", acceleration.x];
    xBar.progress = ABS(acceleration.x);

    yLabel.text = [NSString stringWithFormat:@"%f", acceleration.y];
    yBar.progress = ABS(acceleration.y);

    zLabel.text = [NSString stringWithFormat:@"%f", acceleration.z];
    zBar.progress = ABS(acceleration.z);
}

@end
1

The UIAccelerometer is a singleton object, so we grab a reference to the singleton rather than allocate and initialize a new instance of the class.

2

We set the update interval to 0.1, hence the accelerometer:didAccelerate: method will be called 10 times every second.

3

We declare that this class is the delegate for the UIAccelerometer.

4

We implement the accelerometer:didAccelerate: delegate method and use it to set the X, Y, and Z labels to the raw accelerometer readings each time it is called. The progress bar values are set to the absolute value (the value without regard to sign) of the accelerometer reading.

OK, you’re done. Before you click the Run button, make sure you’ve configured the project to deploy onto your iPhone or iPod touch to test it. Since this application makes use of the accelerometer, and iPhone Simulator doesn’t have one, you’re going to have to test it directly on the device.

If all goes well, you should see something that looks a lot like Figure 4-4.

The Accelerometer application running on an iPhone 4 sitting face-up on my desk, measuring a 1-gravity acceleration straight down

Figure 4-4. The Accelerometer application running on an iPhone 4 sitting face-up on my desk, measuring a 1-gravity acceleration straight down

Get Basic Sensors in iOS 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.