Search code examples
objective-cmacoscocoansnotification

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

}

Solution

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