3.10. Application Badges

With the iPhone's numerous different connections—3G/EDGE, WiFi, and Bluetooth—lots of things can happen while you've got that little device stuck in your pocket. Without some notification to the user that there are pending notifications, he's likely to miss everything that's happened while he was busy having a real life. As new features appear in iPhone firmware, such as push notification, your application may receive new information when it is not in the foreground. In addition to this, application badges can serve as a reminder to a user who has run the application but did not finish viewing all messages, notifications, or other data. Application badges are small message bubbles that appear on the program's home screen icon. Application badges are used heavily by Apple's preloaded applications to alert the user to missed calls, voicemail, text messages, and email.

One of the nice features about these types of badges is that the application doesn't necessarily need to be running for the badge to display on the springboard. This is useful in serving as a reminder to the user even after he has exited the application. This also means you'll need to clean up any lingering badges when your program exits.

3.10.1. Displaying an Application Badge

Application badges are one of the easier features to take advantage of, requiring only one call to the UIApplication class:

[ UIApplication sharedApplication ].applicationIconBadgeNumber = 42;

The applicationIconBadgeNumber property takes an NSInteger object, so you can only set numeric integer values with it, but this is often enough to convey the message that a certain number of new items are waiting.

3.10.2. Removing an Application Badge

An application badge should be removed when the user has clicked to the page with the important events he was being notified about. Removing the application badge is also an easy task. The badge is erased when its value is set to zero. A good place to put such code is after transitioning to the controlling view displaying the new events:

[ UIApplication sharedApplication ].applicationIconBadgeNumber = 0;

An application badge will continue to hang around even after an application has terminated. This can be useful, but is not always what you want. If an application badge should be removed when the program exits, set this property inside the application delegate's applicationWillTerminate method:

- (void)applicationWillTerminate {
    /* We are about to exit, so remove the application badge */

    [ UIApplication sharedApplication ].applicationIconBadgeNumber = 0;

3.10.3. Further Study

Before going on to learn some of the situations in which application badges can help improve your application's response to state changes, do a little exploration:

  • Experiment and determine the maximum number of digits that can be added to an application badge. What happens when you exceed this limit?

  • Check out the UIApplication.h prototype in your SDK's header files. You'll find these under /Developer/Platforms/iPhoneOS.platform, inside the UI Kit framework's Headers directory.