Hi there! Please advice how I can subscribe to listening system events (Lock Screen/User log off/Device mounted/etc)? I try to use NSDistributedNotificationCenter to do it, but I can receive only own notifications =(
This is the my test Console application:
//
// main.m
// EventsListener
//
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#include <iostream>
@interface MyClass : NSObject
-(void)subscribe;
-(void)handleNotification:(NSNotification*)notification;
@end
@implementation MyClass : NSObject
-(void)subscribe
{
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:nil object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(handleNotification2:) name:nil object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification3:) name:nil object:nil];
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification4:) name:@"com.apple.screenIsLocked" object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(handleNotification4:) name:@"com.apple.screenIsLocked" object:nil];
}
-(void)handleNotification:(NSNotification*)notification
{
NSLog(@"handleNotification: %@", notification);
}
-(void)handleNotification2:(NSNotification*)notification
{
NSLog(@"handleNotification2: %@", notification);
}
-(void)handleNotification3:(NSNotification*)notification
{
NSLog(@"handleNotification3: %@", notification);
}
-(void)handleNotification4:(NSNotification*)notification
{
NSLog(@"handleNotification3: %@", notification);
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
MyClass* mc = [MyClass new];
[mc subscribe];
[[NSNotificationCenter defaultCenter] postNotificationName:@"test notification" object:nil];
NSLog(@"Press enter to abort");
getchar();
}
return 0;
}
What wrong I does? Thanks!