27

How do I add a badge to the Dock icon for my app using Cocoa? Is there any roughly "standardized" way to do this?

(I'm referring to the kind of badges that show up in Mail, iChat etc. to indicate the number of unread messages and the like.)

Cocoa Touch does provide one such method, but I haven't been able to find any equivalent for a regular Cocoa application.

Marek H
  • 5,173
  • 3
  • 31
  • 42
Debajit
  • 46,327
  • 33
  • 91
  • 100
  • Great resource is also WWDC 2003 Session 424 - Cocoa: Tips and Tricks https://youtu.be/Y3jYF0EhxOg?t=1099 – Marek H Mar 08 '19 at 11:13

3 Answers3

27

Use

 [[[NSApplication sharedApplication] dockTile] setBadgeLabel:@"2234"];

This method, and the NSDockTile class, has been available since Leopard.

Tristan
  • 3,058
  • 6
  • 40
  • 68
25

It should be noted that NSDockTile is only available on Leopard. If you need to target Tiger you'll need to use -setApplicationIconImage: on your NSApplication object and draw your badge by hand.

Also, it's not in the documentation outside of the release notes that I could find but you get your application's dock tile by sending the dockTile message to your NSApplication object.

NSDockTile *tile = [[NSApplication sharedApplication] dockTile];
[tile setBadgeLabel:@"Lots"];
Ashley Clark
  • 8,813
  • 3
  • 35
  • 35
  • Realistically, with 10.6 coming out in the next 6 months or less, only supporting Leopard is probably just fine. – wfarr Dec 26 '08 at 06:34
  • 1
    In yours and my case maybe so, but Debajit should at least be aware of the implications of using NSDockTile and the other available options. – Ashley Clark Dec 26 '08 at 08:44
17

A quick google search turned up the NSDockTile class. Seems pretty self-explanatory once you take a gander at the documentation.

wfarr
  • 1,563
  • 1
  • 13
  • 13