Applications
on the iPhone are user-centered, which means they typically don’t
perform utility functions in the background or operate without an
interface. They enable users to work with data, play games, communicate,
or carry out dozens of other activities. Despite the variation in
activities, when an application needs to show a warning, provide
feedback, or ask the user to make a decision, the iPhone does so in a
common way. Cocoa Touch leverages a variety of objects and methods to
gain your attention, including UIAlertView, UIActionSheet, and System Sound Services.
This hour explains how you can implement these notification features into your application.
By the Way
Note that I said applications typically
don’t operate in the background? That’s because, with iOS 4, some do!
Applications running in the background have a unique set of
capabilities, including additional types of alerts and notifications.
Prepping the Notification Project Files
To practice using these
alert classes and methods, we need to create a new project with buttons
for activating the different styles of notifications. Open Xcode and
create a new project based on the View-Based Application iPhone
template. Name the project GettingAttention.
Within Xcode, open the GettingAttentionViewController.h file and add the outlets and actions shown in Listing 1
Listing 1.
#import <UIKit/UIKit.h>
@interface GettingAttentionViewController : UIViewController { IBOutlet UILabel *userOutput;
}
@property (retain, nonatomic) IBOutlet UILabel *userOutput;
- (IBAction)doAlert:(id)sender; - (IBAction)doMultiButtonAlert:(id)sender; - (IBAction)doAlertInput:(id)sender; - (IBAction)doActionSheet:(id)sender; - (IBAction)doSound:(id)sender; - (IBAction)doAlertSound:(id)sender; - (IBAction)doVibration:(id)sender;
@end
|
The first outlet, userOutput,
will be implemented as a text label for providing simple feedback
within the application. The seven actions are methods that correspond to
the different notification methods we’ll be writing throughout the
hour.
Next, edit the start of the GettingAttentionViewController.m file and add the following code after the existing @implementation line:
@synthesize userOutput;
-(IBAction)doAlert:(id)sender {
}
-(IBAction)doMultiButtonAlert:(id)sender {
}
-(IBAction)doAlertInput:(id)sender {
}
-(IBAction)doActionSheet:(id)sender {
}
-(IBAction)doSound:(id)sender {
}
-(IBAction)doAlertSound:(id)sender {
}
-(IBAction)doVibration:(id)sender {
}
The @synthesize directive is used to create the getter/setter for the userOutput text label. Next, seven stub methods are defined for our actions. Finally, be sure to edit the dealloc method to release the userOutput object:
- (void)dealloc {
[userOutput release];
[super dealloc];
}
That completes the code
skeleton that we’ll be using throughout this hour. Now let’s create the
interface in Interface Builder and connect the outlets and actions.
Creating the Notification Project Interface
Open the
GettingAttentionViewController XIB file in Interface Builder. We need to
add seven buttons and a text label to the empty view. You should be
getting quite familiar with this process by now. Just follow these
steps:
1. | Double-click the View icon in the Document window to open the empty view.
|
2. | Add a button to the view by opening the library (Tools, Library) and dragging a Round Rect Button (IUButton) to the View window.
|
3. | Add five more buttons, spaced out evenly below the first. Make sure you leave room at the bottom for a label.
|
4. | Change
the button labels to correspond to the different notification types
that we’ll be using. Specifically, name the buttons (top to bottom) Alert Me!, Alert with Buttons!, I Need Input!, Lights, Camera, Action Sheet, Play Sound, Play Alert Sound, and Vibrate Phone.
|
5. | Drag a label (UILabel)
from the library to the bottom of the view. Remove the default label
text and set the text to align center. The interface should resemble Figure 1.
|
Connecting the Outlets and Actions
The interface itself is finished, but we still need to make the connection to our properties and method stubs, as follows:
1. | Select the first button (Alert Me!), and then press Command+2 to open the Connection Inspector.
|
2. | From the Touch Up Inside connection point, click and drag to the File’s Owner icon in the Document window.
|
3. | When prompted, choose the doAlert method from the list (see Figure 2).
|
4. | Repeat this pattern for the other six buttons. Alert with Buttons! connects to doMultiButtonAlert; I Need Input! should connect to the doAlertInput method; Lights, Camera, Action Sheet to doActionSheet; Play Sound to doSound; Play Alert Sound to doAlertSound; and Vibrate Phone to doVibration.
|
5. | To
connect the label, Control-drag from the File’s Owner icon in the
Document window to the label (either in the View window or the View
hierarchy in the Document window). Choose the userOutput outlet to make the final connection, as demonstrated in Figure 3.
|
The framework for our test of notification is ready. We’ll start by implementing a simple alert view.