How to process notifications from NSNotificationCenter in QT application? - objective-c

Have a simple QT console app, with QCoreApplication.
Is it possible to process notifications from notification center in another thread? For example create a thread and connect loop of this thread to process notifications? Or extend QT application to send me this notifications?

There are plenty of example in the source of Qt itself.(I did "git grep NSNotificationCenter" here.)
At least you could check the solution like following:
https://github.com/qtproject/qtbase/blob/stable/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm
L105-L109:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(updateScreens:)
name:NSApplicationDidChangeScreenParametersNotification
object:NSApp];
L130:
[[NSNotificationCenter defaultCenter] removeObserver:self];

Related

Why can't I observe NSApplicationXYZNotification with addObserverForName?

I'm trying to make an Objective-C iOS library work for macOS applications. Removing UIKit-based notifications is the only real task here. When trying to replace—for example—UIApplicationDidEnterBackgroundNotification with NSApplicationDidResignActiveNotification, I run into the odd error of the latter variable being called an undeclared identifier.
[self.observers addObject: [[NSNotificationCenter defaultCenter] addObserverForName:NSApplicationDidResignActiveNotification object:nil queue:nil usingBlock:^(NSNotification* note) {
// stop operations
}]];
The same error does not arise when I use the alternate method:
[[NSNotificationCenter defaultCenter] addObserver:[???] selector:[some selector] name:NSApplicationDidResignActiveNotification object:nil];
The problem with this method though is that I have to have an observer beforehand—like self—as opposed to receiving one that I can add to the self.observers array.
I've read a smattering of docs, questions, and guides—including the NSApplication doc and the NSHipster guide—but I can't seem to figure out my misunderstanding, though I believe it is something fundamental about NSNotificationCenter and how it works.
Silly solution, though aided by #Willeke's comment that they weren't seeing an error with the same code snippet—the library needed Cocoa.h in place of the iOS imports.

Get EAAccessoryDidConnectNotification when app is in background (iOS)

In my 'viewDidLoad'in ViewController.m, I am registering to the NSNotificationCenter defaultCenter with 'EAAccessoryDidConnectNotification' and 'EAAccessoryDidDisconnectNotification'
When my app is active in foreground, I get the notification, and respond in accessoryDidConnect. all works OK.
But, when app is in background, how can I get such notification?
('EAAccessoryDidConnectNotification' and 'EAAccessoryDidDisconnectNotification')
[Code below]
Thanks a lot.
Dan
(void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(accessoryDidConnect:)
name:EAAccessoryDidConnectNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(accessoryDidDisconnect:)
name:EAAccessoryDidDisconnectNotification
object:nil];
[[EAAccessoryManager sharedAccessoryManager] registerForLocalNotifications];
}
According to the documentation, it's not possible:
If your app is suspended in the background when an accessory
notification arrives, that notification is put in a queue. When your
app begins running again (either in the foreground or background),
notifications in the queue are delivered to your app. Notifications
are also coalesced and filtered wherever possible to eliminate any
irrelevant events. For example, if an accessory was connected and
subsequently disconnected while your app was suspended, your app would
ultimately not receive any indication that such events took place.
But it would be interesting if someone proved me wrong.
in iOS 12.1.4, plug in the relay cable with iPhone and DSLR Camera when app is in foreground, then switch app into background mode, unplug the relay cable and switch app back into foreground, you will receive the accessoryDidDisconnect notify via EAAccessory's delegate methods.

Notified with changes of Calendar events

I want to get notified when my application is in background with :
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(calendarChanged:) name:EKEventStoreChangedNotification object:nil];
I tried to implement it but (calendarChanged:) never get called ?!
You should specify the EKEventStore object when registering the notification observer
Your method is not going to be called while in background, it will be called once your app comes to foreground.
Taken from this question, the search option doesn't hurt.

Is there a way to recognise from which application NSNotification / NSEvent came?

I have started listening to global keyDown events. Is there a way to get information from which application that event came?
A handler receives NSNotification instance and NSEvent is part of it. Can I somehow extract that information from those objects?
Listening snippet:
[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyDownMask handler:^(NSEvent *event){
NSLog(#"global keyDown %#", event);
[[NSNotificationCenter defaultCenter] postNotificationName:kKeyPressed
object:event];
}];
Observer:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyEventHandler:)
name:kKeyPressed
object:nil];
UPDATE
Global key downs are not send from any particular application. What I actually needed is to check for currently active application at event handler:
[[NSWorkspace sharedWorkspace] activeApplication]
This returns NSDictionary with information I needed.
You're not posting a distributed notification, or using a distributed notification center. This means you know that the notification came from the current application.
Meanwhile, you're generating the notifications yourself, so if you did need to know the application, you could just add that in.
Finally, the events you're embedding are global key events, which do not have an associated application. Except in special cases, they're not generated by any application, they're generated by the user typing on the keyboard.

[NSWorkspace notificationCenter] vs. NSDistributedNotificationCenter

What is the difference between using [NSWorkspace notificationCenter] or using NSDistributedNotificationCenter? What are they intended to be used for and what are the pros and cons of each?
I am trying to understand this by reading the Apple documentation but it doesn't shed much light on it. It says that [NWorkspace notificationCenter] "Returns the notification center for workspace notifications". How would I know what is would be a workspace notification? I have an observer that observes changes in the active app and uses a workspace notification:
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:#selector(windowChangeNotification:) name:NSWorkspaceDidActivateApplicationNotification object:nil];`
But I've read sample code that mentioned that if you want to detect the screen turning off you would use NSDistributedNotificationCenter
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:#selector(screenIsLocked:) name:#"com.apple.screenIsLocked" object:nil];
I would have thought you would use similar ways to detect these notifications. But you use different notification centers. Can someone help me out?