Using Application Badges

Wed, Jan 14

Several native applications on the iPhone use application badges as an indicator of new messages, think email and SMS. Creating badges is quite straightforward and is nothing more than a method call, passing in the desired number to display.

The image below shows how a badge may look when applied to your application. The code to create the badge is below the image.

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:99];

As one would expect, the iPhone does limit the number of digits it will display – see the code and image that follow:

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:123456];

One nice feature that would be welcome is to have a means to create a badge with text. I’ve seen a number of references to using an undocumented method (see below), however, I was unable to get the code to work.

[[UIApplication sharedApplication] setApplicationBadge:@"Beer"];

Although you can set a badge for an application icon, I believe the real creative uses of this will evolve if/when Apple provides a means for an application to update the badge when the application is not running. For example, I am working on an application that needs to notify users that they are x number of days away from an upcoming event. It would be a nice feature of the application if one could glance at the icon and see the current count, versus having to start the application.

I’ll keep my fingers crossed…

13 comments

Hey John,

As someone who is new xcode thow would I pass a variable to the App Badge?

Thanks, Doug

by Doug on Feb 25, 2009. #

Hi Doug,

You should be able to pass in any variable of type NSInteger.

NSInteger x = 99;
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:x];

John

by John on Feb 25, 2009. #

Hi, can you show negative number? That way, temperature could be shown (for an online weather app)

by Bjørn Tennøe on Feb 16, 2010. #

Hi Bjørn,

Unfortunately, it looks like zero or any negative value removes the badge from the display.

by John Muchow on Feb 16, 2010. #

Is it possible to use badges on a segmented control? We’re working on an app where one of the buttons opens a comments pane. I’d like to use a badge to display a count of new comments since last viewed. I’ve been told this isn’t possible.

by Nick Smith on Apr 27, 2010. #

Hey Nick,

You could achieve that by positioning a custom UIView with badge image and label over the segmented control or use the method setImage:forSegmentAtIndex: to set a custom image as the segments content. You could create the segment content as a custom UIView then render it as an image (checkout http://pastie.org/244916) then set that as the segment image. Maybe create a UIView subclass with a method that takes a comment number and returns a UIImage to set as the segment control content when the comment count changes?

by Tom H on May 8, 2010. #

Hi,

How do you update the badge when your app is inactive? I want my app to check msgs and update the badge but once I close the app, the timer stops.
Thanks.

by John on Oct 15, 2010. #

John, you will need to look into the multi-tasking API’s to manage this, by default once something is put into the background, it is essentially paused.

by John Muchow on Oct 17, 2010. #

You can schedule that you get an update using UILocalNotification. The class reference discusses the options for it, but basically you can schedule when your app receives a message. This posting is also very helpful: http://stackoverflow.com/questions/5375355/clear-app-badge-with-local-notifications

by Keith on May 4, 2011. #

To use the a zero or a negative number, would you be able to use for example x=(0) or x=(-9) with the parentheses keeping it from actually being a true zero or negative number. Just a thought.

by James Heriot on Oct 13, 2011. #

“Although you can set a badge for an application icon, I believe the real creative uses of this will evolve if/when Apple provides a means for an application to update the badge when the application is not running.”

The above is possible using APNS, but that requires a server application to keep track of your data and that makes your application far more complicated.

by Kasper Munck on Feb 13, 2012. #

when first time application installed badge count is 0. Now in my application badge count is 20. now i remove application from spring board and again installed that time i want show badge count 0. but it’s showing 20.

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

i write this code when app is launched.

Thanks

by sreenivas on Mar 19, 2012. #

@Srenivas

Find this Method in appDelegate.m

– (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
// I have added this line, number on App Icon shows Chapter number
[UIApplication sharedApplication].applicationIconBadgeNumber = 7;
}

everytime you will change value that will be updated

by Alix on Nov 14, 2012. #