Reputation: 36
Not very much accustom to Objective C, hence trying to observe NSWorkspaceDidActivateApplicationNotification
event in C++ code with CFNotificationCenterAddObserver()
method.
#include<CoreFoundation/CFNotificationCenter.h>
#include<iostream>
#include<QGuiApplication>
void Callback (CFNotificationCenterRef center, void* observer, CFNotificationName name,
const void* object,
CFDictionaryRef userInfo)
{
std::cout << "Hello World\n";
//NSLog(@"New application: %@", [[name userInfo] objectForKey:NSWorkspaceApplicationKey]);
}
int main (int argc, char** argv)
{
QGuiApplication application(argc, argv);
std::cout << "Add observer\n";
CFNotificationCenterAddObserver(CFNotificationCenterGetDistributedCenter(), nullptr, Callback,
CFStringRef("NSWorkspaceDidActivateApplicationNotification"), nullptr,
CFNotificationSuspensionBehaviorDeliverImmediately);
std::cout << "Added ...\n";
return application.exec();
}
However it results in below crash. If I try to run in debug mode, then it sometimes shows the below crash or sometimes get stuck in that function itself.
Add observer
2021-12-03 21:44:24.527086+0530 Test[17288:982994] -[__NSTaggedDate length]: unrecognized selector sent to instance 0x100007c65
2021-12-03 21:44:24.527916+0530 Test[17288:982994] [General] An uncaught exception was raised
2021-12-03 21:44:24.527943+0530 Test[17288:982994] [General] -[__NSTaggedDate length]: unrecognized selector sent to instance 0x100007c65
2021-12-03 21:44:24.528044+0530 Test[17288:982994] [General] (
0 CoreFoundation 0x00007fff35570035 __exceptionPreprocess + 256
1 libobjc.A.dylib 0x00007fff5fc80a17 objc_exception_throw + 48
2 CoreFoundation 0x00007fff355e9fce -[NSObject(NSObject) __retain_OA] + 0
3 CoreFoundation 0x00007fff35511e9f ___forwarding___ + 1485
4 CoreFoundation 0x00007fff35511848 _CF_forwarding_prep_0 + 120
5 CoreFoundation 0x00007fff355aca4c __CFXNotificationRegisterObserver + 588
6 CoreFoundation 0x00007fff3548a037 CFNotificationCenterAddObserver + 204
7 Test 0x00000001000066a1 main + 129
8 libdyld.dylib 0x00007fff614503d5 start + 1
)
2021-12-03 21:44:24.528241+0530 Test[17288:982994] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSTaggedDate length]: unrecognized selector sent to instance 0x100007c65'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff35570035 __exceptionPreprocess + 256
1 libobjc.A.dylib 0x00007fff5fc80a17 objc_exception_throw + 48
2 CoreFoundation 0x00007fff355e9fce -[NSObject(NSObject) __retain_OA] + 0
3 CoreFoundation 0x00007fff35511e9f ___forwarding___ + 1485
4 CoreFoundation 0x00007fff35511848 _CF_forwarding_prep_0 + 120
5 CoreFoundation 0x00007fff355aca4c __CFXNotificationRegisterObserver + 588
6 CoreFoundation 0x00007fff3548a037 CFNotificationCenterAddObserver + 204
7 Test 0x00000001000066a1 main + 129
8 libdyld.dylib 0x00007fff614503d5 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
How to fix this?
Upvotes: 2
Views: 317
Reputation: 6290
Use CFSTR macro to create a CFStringRef from a constant C string like so:
CFSTR("NSWorkspaceDidActivateApplicationNotification")
NSWorkspaceDidActivateApplicationNotification docs says that you have to use NSWorkspace.notificationCenter to receive it, CFNotificationCenterGetDistributedCenter won't work.
Upvotes: 1