MOBILE

iPhone Application Development : Getting the User’s Attention - Exploring User Alert Methods

2/19/2011 3:41:38 PM
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.



Figure 1. Create an interface with seven buttons and a label at the bottom.


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).

Figure 2. Connect the buttons to the method stubs.

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.

Figure 3. Connect the userOutput outlet to the label in the view.

The framework for our test of notification is ready. We’ll start by implementing a simple alert view.

Other  
  •  iPhone Application Development : Using Advanced Interface Objects and Views - Using Scrolling Views
  •  Working with the Windows Phone 7 Application Life Cycle (part 2) - Managing Application State
  •  Working with the Windows Phone 7 Application Life Cycle (part 1) - Observing Application Life Cycle Events
  •  Mobile Application Security : SymbianOS Security - Development and Security Testing
  •  Mobile Application Security : SymbianOS Security - Introduction to the Platform
  •  Java Mobile Edition Security : Permissions and User Controls
  •  Integrating Applications with the Windows Phone OS : Working with Launchers and Choosers
  •  Introducing Windows Phone 7 Launchers and Choosers
  •  Java Mobile Edition Security : Development and Security Testing (part 3) - Code Security & Application Packaging and Distribution
  •  Java Mobile Edition Security : Development and Security Testing (part 2) - Reverse Engineering and Debugging
  •  Java Mobile Edition Security : Development and Security Testing (part 1) - Configuring a Development Environment and Installing New Platforms & Emulator
  •  Java Mobile Edition Security : Configurations, Profiles, and JSRs
  •  Programming the Mobile Web : Performance Optimization
  •  Programming the Mobile Web : Testing and Debugging (part 3) - Client-Side Debugging
  •  Programming the Mobile Web : Testing and Debugging (part 2) - Server-Side Debugging & Markup Debugging
  •  Programming the Mobile Web : Testing and Debugging (part 1) - Remote Labs
  •  Windows Phone 7 : Working with Controls and Themes - Adding Transition Effects
  •  Windows Phone 7 : Working with Controls and Themes - Understanding Frame and Page Navigation
  •  Windows Phone 7 : Working with Controls and Themes - Panorama and Pivot Controls
  •  Programming the Mobile Web : Widgets and Offline Webapps - Platforms (part 5) - Windows Mobile & BlackBerry
  •  
    Top 10
    Sapphire Pure Platinum A85XT - Sensible Smattering Of Features
    SSD Supertest – December 2012 (Part 6)
    SSD Supertest – December 2012 (Part 5) : Samsung 840 250GB, Samsung 840 Pro 256GB
    SSD Supertest – December 2012 (Part 4) : OCZ Vertex 4 256GB, Plextor M5 Pro 128GB
    SSD Supertest – December 2012 (Part 3) : KingSpec PCIe MultiCore 1TB, Kingston HyperX 3K 120GB
    SSD Supertest – December 2012 (Part 2) : Intel 330 Series 180GB
    SSD Supertest – December 2012 (Part 1) : Corsair Neutron 240GB, Corsair Neutron GTX 240GB
    Synology DS213+ - Great Features, But Expensive
    Thermaltake Level 10m - The BMW Gaming Mice
    ZOTAC GTX 650 TI – It’s Time To Play
    Most View
    Asus X501A - Thin And Light Design For Your Personal Expression
    Personalize Your iPhone Case
    SharePoint 2010 : Outlining the Inherent Threat in SharePoint Web Traffic
    Displacement Maps
    Google’s Data Liberation Front (Part 2)
    The big test … Inter Core Power (Part 2) - Asus Zenbook UX31E & Dell XPS 14z
    How To Automate Your Web With ifttt (Part 1)
    HP Photosmart 5510 e-All-in-One Printer
    OData with SQL Azure - Enabling OData on an Azure Database
    Algorithms for Compiler Design: DATA STRUCTURES FOR REPRESENTING PARSING TABLES
    Recommended Buys: Everyday Computing – November 2012
    Asus X53z
    The New Sheriff In Town
    Which Is The Real Best-Seller Ultrabook? (Part 1) - Asus Zenbook Prime, Acer Aspire S5, HP Folio 13-2000
    Exchange Server 2007: Design and Deploy Disaster Recovery Settings - Implement Database Portability
    Gioteck HF-2 Controller - Different Strokes
    Managing Exchange Server 2010 : The Exchange Management Shell (part 2) - Remote PowerShell
    The Pick Of Windows 8 Metro Apps (Part 2)
    HP Global Influencer Summit 2012 (Part 2) - HP Photosmart 5520 e All-in-One, HP t410 AIO
    Google’s Data Liberation Front (Part 1)