Wednesday, December 8, 2010

How to add a badge to the application icon


You’ve seen many times the Mail icon on your home screen featuring a red badge with the unread message you currently have in your Inbox. That’s really cool effect while you can effectively keep track of this number without having to start the app to check it. Now in your own app this number might be something else – unread RSS items, unanswered chat messages, or for example might be something totally different (not necessarily useful) – for example the days since your app was installed on the device – how about that?
Anyhow – putting the badge on the icon is really easy.

Put this number on that icon

Since iOS4 the UIApplication class has a property called applicationIconBadgeNumber. You can assign any integer to that property, although my test show that if you assign anything less than 1 (ie zero or negative integers) there just won’t be any badge on your icon. (NB: pay attention only iOS4 solution)
Here’s a short example code how to set the number on the badge:
[UIApplication sharedApplication].applicationIconBadgeNumber = 23;

Example app

You can download my example XCode project and play with it to check for yourself how the badges look like. The demo app will show you the current badge number with couple of buttons to increase and decrease that number. When you change the number, quit the app and have a look at the app icon- nice!

Download badge test project source code here
 
If you liked this article please share it with your friends or colleagues; there's also a low traffic RSS feed to give you heads up when there's a new post in Touch Code Magazine.

No comments:

Post a Comment

Followers