Search code examples
objective-cxcode4

How to create a background only application in xcode?


That show up on menu bar

It's like this question: How to create a background-running Cocoa application?

However he ask how to make something that doesn't show up on menu bar. Well I want things to show up on menu bar.


Solution

  • The key things you want to do after starting a new Cocoa application in Xcode:

    1) Give one of your classes an NSStatusItem @property and an IBOutlet NSMenu @property and initialize them once the application has started in your AppDelegate's awakeFromNib (of course, it doesn't have to be the AppDelegate that controls the status item; you could use another class):

    self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
    [self.statusItem setMenu:self.menu];
    [self.statusItem setTitle:@"Status"];
    [self.statusItem setHighlightMode:YES];
    

    2) In your MainMenu.xib, make an NSMenu and wire it up to your AppDelegate's NSMenu IBOutlet. This will be the menu that shows up when you click on your status item, so add any additional items you want, make IBActions for them, and wire them up.

    3) Also in your MainMenu.xib, delete the Window object in MainMenu.xib because you don't need a main window to show up when you start the application.

    4) In your info.plist, add the key "Application is agent (UIElement)" and set its value to YES. This gets rid of the dock icon.

    The tutorial that GravityScore linked to covers the same material as above. If after reading the above you're not sure how you would set up actions for the menu items, or want to know how to dynamically add menu items, this other tutorial might be helpful.

    And of course, see the NSStatusItem Class Reference for how to customize the status item's appearence.