3

my dock menu always be added "Quit" and other 2 menu items automatically, how may I block / modify them?

updated:

really NO way to delete/block/redirect the "Quit" menu item. used Peter's recommendation at last like blow hope helpful to others

-(NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
    if (needPassword)
    {
        [self checkPassword:self];
        return  NSTerminateCancel;
    }
    else 
    {
        return NSTerminateNow;
    }
}


-(void)checkPassword:(id)sender
{
    if(passwordCorrect)
    {   
        !needPassword;
            [[NSApplication sharedApplication] terminate:self];
    }
}
Duck
  • 34,902
  • 47
  • 248
  • 470
Jiulong Zhao
  • 1,313
  • 1
  • 15
  • 25

2 Answers2

8

Trying to intercept all possible ways the user might tell your application to quit is bound to fail. (Did you remember the Quit Apple Event?)

It'll be both easier and more effective to just implement the applicationShouldTerminate: method in your application's delegate. Put up the password panel and return NSTerminateLater. Then, when the user either enters the correct password or cancels, send the application a replyToApplicationShouldTerminate: message.

Whichever Quit commands (menu items, etc.) you've already ripped out, put them back. Let the user invoke the normal Quit command in the normal way; that will trigger the aforementioned should-terminate procedure to determine whether the quit will actually happen.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • Yes, Peter, you are right! but I really found some apps killed those menuitems. e.g. google earth, or this free app with source "NOTATIONAL VELOCITY", can you please find it out how? sorry I do not have the permit uploading image. – Jiulong Zhao Dec 08 '11 at 06:31
  • And if you really don't want users to be able to quit, don't forget to install a signal handler. – sbooth Dec 08 '11 at 12:39
  • @JiulongZhao: Google Earth does have a Quit menu item. I've never used Notational Velocity, but can't think of any reason why it shouldn't. More generally, there is no reason to remove the Quit menu items. – Peter Hosey Dec 08 '11 at 21:02
  • Peter, just remove "Quit" from "Dock-menu" only. No one like to be adding something by force... – Jiulong Zhao Dec 09 '11 at 00:10
  • after `NSTerminateLater` and before `replyToApplicationShouldTerminate`, right click quit from dock doesn't work – jimwan Dec 01 '16 at 08:05
1

1)Open the MainMenu.xib 2)Create your own dock menu 3)Right click on the File's Owner (NSApplication instance) 4)Connect the property "dockMenu" with your custom menu

If you want to do that because of learning purposes it's fine. However, when you want to sell this application you should reconsider this. Users expect your app to have a quit button in the dock menu.

lbrndnr
  • 3,361
  • 2
  • 24
  • 34
  • Hi Larus94, I just did like what you said, but the system always add "Quit","Show All Windows" and "Hide" 3 menu items automatically to your dock menu! My app is password protected and can not terminate like that. – Jiulong Zhao Dec 08 '11 at 04:30
  • Did you guys get any solution for this issue. Even i am trying to achieve the same of removing "Show All Windows" from the dock menu.. – sac Jun 10 '15 at 05:39