0

I want to register for time change notifications in mac os. usng C Api's. I tried the ApplicaitonEventHandler api which seemed to be the api for this case, but I am not getting notified for any events

code shared at,

https://gist.github.com/anonymous/a0b3e24e02b844bfb11e2b5d02e47d83

I registered for all possible event but triggering any of the event doesnt seem to create a notificaion for me.

I am using .mm extension as I want my code exposed as a c style api so and if I can interop with a private objective c class if possible (i.e if objective c object can be instantiated inside a c++ method and a suitable objective c api is available for such notifcations).

[Note] Used the NSNotification suggested below, it doesnt send any notifcations either.

user3279954
  • 556
  • 2
  • 7
  • 22
  • I registered for the nsnotification from the sample cocoa application and the observer is notified as expected. Is this a limitation for console apps or am I doing something wrong ? https://gist.github.com/anonymous/b7f9f9d0402447c49c59e30f0a6e8977 – user3279954 Jul 13 '17 at 01:28

1 Answers1

0

If you just want to be notified when the clock is changed, you can listen to the com.apple.MenuBarClock.ClockNotification.

[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(timeChanged:) name:@"com.apple.MenuBarClock.ClockNotification" object:nil suspensionBehavior:NSNotificationSuspensionBehaviorDeliverImmediately];
Stefanf
  • 1,663
  • 13
  • 15
  • https://gist.github.com/anonymous/b7f9f9d0402447c49c59e30f0a6e8977 @stefanf used the NSNotificationCenter, and distributednotification center both doesnt yeild the callback. – user3279954 Jul 12 '17 at 23:36
  • I created a simple application using xcode, IS there anything my console app needs to do to start receiving these notifications ? – user3279954 Jul 12 '17 at 23:37
  • Added an objective-c sample to your gist. – Stefanf Jul 13 '17 at 10:24
  • Thanks for the sample, is there a way to do this without sharedApplication? sharedApplication seems to be singleton and may be shared by other modules in the process. https://developer.apple.com/documentation/uikit/uiapplication/1622975-sharedapplication?language=objc – user3279954 Jul 13 '17 at 20:26
  • Found this: https://stackoverflow.com/questions/2154600/run-nsrunloop-in-a-cocoa-command-line-program – Stefanf Jul 13 '17 at 21:36