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];
}
}
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.