How to find entry and exit events in Mac?

I am useful for this area of ​​application development. I am trying to get notifications during user login and logout. I tried with NSWorkSpaceNotifications , but it does not work for me.

Can someone help me.

 -(void)logInLogOutNotifications{ NSNotificationCenter *notCenter; notCenter = [[NSWorkspace sharedWorkspace] notificationCenter]; [notCenter addObserver:self selector:@selector(observerMethod) name:NSWorkspaceWillPowerOffNotification object:nil]; } -(void)observerMethod:(NSNotification *)senderNotification;{ NSLog(@"System Logout Notification is called***********************"); } 
+4
source share
4 answers

NSApplicationMain Start RunLoop . You call the logInLogOutNotifications function from main (), so you run runloop. or call logInLogOutNotifications in applicationDidFinishLaunching

 -(void)logInLogOutNotifications{ NSNotificationCenter *notCenter; notCenter = [[NSWorkspace sharedWorkspace] notificationCenter]; [notCenter addObserver:self selector:@selector(observerMethod) name:NSWorkspaceWillPowerOffNotification object:nil]; [[NSRunLoop currentRunLoop] run]; } 
+3
source

Your method takes one argument, so you must change

 @selector(observerMethod) 

to

 @selector(observerMethod:) 
+3
source

If you are like me and don’t understand why you don’t receive NSWorkspaceWillPowerOffNotification notification in the NSWorkspaceWillPowerOffNotification , make sure you configure yourself as an observer [[NSWorkspace sharedWorkspace] notificationCenter] and -NOT- [NSNotificationCenter defaultCenter] !

I spent most of the day trying to debug why I did not receive this notification because I did not read the answer clearly!

+2
source

If you want to know when a user resigns or becomes active, you must subscribe to the following notifications:

Swift 3

  NSWorkspace.shared().notificationCenter.addObserver(self, selector: #selector(sessionResignActive(_:)), name: NSNotification.Name.NSWorkspaceSessionDidResignActive, object: nil) NSWorkspace.shared().notificationCenter.addObserver(self, selector: #selector(sessionBecomeActive(_:)), name: NSNotification.Name.NSWorkspaceSessionDidBecomeActive, object: nil) 
+1
source

Source: https://habr.com/ru/post/1486808/


All Articles