Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find Login & logout events in mac?

I am fresher for this application development field. I am tried to get notifications during user login & logout events. I have tried with NSWorkSpaceNotifications, but it does not working for me.

Can somebody help me please.

-(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***********************");

}
like image 592
RashmiG Avatar asked Dec 05 '25 06:12

RashmiG


2 Answers

NSApplicationMain Begin the RunLoop. You are calling logInLogOutNotifications function from main() function, so you shoud 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];
}
like image 86
Parag Bafna Avatar answered Dec 07 '25 21:12

Parag Bafna


Your method takes one argument, so you should change

@selector(observerMethod)

to

@selector(observerMethod:)
like image 29
boxel Avatar answered Dec 07 '25 20:12

boxel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!