Got same problem, but thanks to JWWalker, documentation and google wrote this code:
// i need to register on button event, you can do it even in applicationDidFinishLaunching
- (IBAction)Btn_LoginAction:(id)sender {
...
NSNotificationCenter *center = [[NSWorkspace sharedWorkspace] notificationCenter];
[center addObserver:self selector:@selector(appLaunched:) name:NSWorkspaceDidLaunchApplicationNotification object:nil];
[center addObserver:self selector:@selector(appTerminated:) name:NSWorkspaceDidTerminateApplicationNotification object:nil];
}
// remember to unregister
- (void)ManageLogout:(NSInteger)aResult {
...
NSNotificationCenter *center = [[NSWorkspace sharedWorkspace] notificationCenter];
[center removeObserver:self name:NSWorkspaceDidLaunchApplicationNotification object:nil];
[center removeObserver:self name:NSWorkspaceDidTerminateApplicationNotification object:nil];
}
- (void)appLaunched:(NSNotification *)note {
[GTMLogger myLog:kGTMLoggerLevelDebug fmt:@"MainWinDelegate::appLaunched: %@ (%@)\n", [[note userInfo] objectForKey:@"NSApplicationBundleIdentifier"], [[note userInfo] objectForKey:@"NSApplicationProcessIdentifier"]];
if ( [[[note userInfo] objectForKey:@"NSApplicationBundleIdentifier"] isEqualToString:@"app.you.monitor.bundle.identifier"] ) {
// do stuff
}
}
- (void)appTerminated:(NSNotification *)note {
[GTMLogger myLog:kGTMLoggerLevelDebug fmt:@"MainWinDelegate::appTerminated: %@ (%@)\n", [[note userInfo] objectForKey:@"NSApplicationBundleIdentifier"], [[note userInfo] objectForKey:@"NSApplicationProcessIdentifier"]];
if ( [[[note userInfo] objectForKey:@"NSApplicationBundleIdentifier"] isEqualToString:@"app.you.monitor.bundle.identifier"] ) {
// do stuff
}
}