Unable to receive NSWorkspaceActiveSpaceDidChangeNotification specifically, but other notifications work - objective-c

-(void)monitor {
NSNotificationCenter *notcenter;
notcenter = [[NSWorkspace sharedWorkspace] notificationCenter];
[notcenter addObserver:self selector:#selector(activeSpaceDidChange:) name:NSWorkspaceActiveSpaceDidChangeNotification object:[NSWorkspace sharedWorkspace]];
- (void) activeSpaceDidChange:(NSNotification *)notification
{
NSLog(#"%#", notification.name);
}
Hey guys, does anyone have any info on this specific notification? For some reason, I seem to be able to get all other notifications, and if I put in nil for the name and receive all NSWorkspace messages, I can get all of them except NSWorkspaceActiveSpaceDidChangeNotification. All code snippets I have seen from people don't seem to be doing anything specific or different than what I have, so I have no idea why I wouldn't be able to get these notifications specifically.

Related

Detecting AUv3 invalidations in a host app (iOS)

I'm working on a AUv3 host app, and I want the users to be notified when a loaded audio unit crashes (gets invalidated) for some reason. I looked at Apple's documentations about AUAudioUnit, but couldn't find any information about how to detect an invalidation of an audio unit (AUv3) from the host app.
Does anyone know how to do this?
Thanks
when setting up your AudioEngine you can observe kAudioComponentInstanceInvalidationNotification to figure out if its audio component instance was invalidated later on.
[[NSNotificationCenter defaultCenter] addObserverForName:kAudioComponentInstanceInvalidationNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
AUAudioUnit *auUnit = (AUAudioUnit *)note.object;
NSValue *val = note.userInfo[#"audioUnit"];
AudioUnit auAddress = (AudioUnit)val.pointerValue;
NSLog(#"AudioComponent Invalidation of Unit:%# with Address:%p", auUnit, auAddress);
}];
keep in mind that you may have got the notification but the component ended up in a deadlock. So when trying to recover from that you may have to turn down the whole host app.

How to observe notifications from a different application?

I'd like to get notified when a certain application triggers an event. I'm not an Objective-C developer nor do I know the OS X APIs—so I hope this question is not too basic.
My goal is to write the meta info of the currently playing song to a log file. For iTunes, I got this working with the following Objective-C line:
[[NSDistributedNotificationCenter defaultCenter]
addObserver: myObserver selector: #selector(observeNotification:)
name: #"com.apple.iTunes.playerInfo" object:nil];
However, I also need this for AirServer (which is an software AirPlay receiver). Unfortunately, the following doesn't work—the observer never gets called:
[[NSDistributedNotificationCenter defaultCenter]
addObserver: myObserver selector: #selector(observeNotification:)
name: #"com.pratikkumar.airserver-mac" object:nil];
So apparently, AirServer does not send these type of notifications. However, there's a notification in the Notification Center when a new song starts playing.
My next step would be to periodically check for new notifications in the OS X Notification Center (as described here: https://stackoverflow.com/a/25930769/1387396). This is not too clean though—so my questions is: is there another option in this particular case?
Catalina silently changed the addObserver behavior - you can no longer use a nil value for the name to observe all notifications - you have to pass in a name. This makes discovery of event names more difficult.
Firstly, you have to understand that while the NSDistributedNotificationCenter has the word Notification in it; it's not related. From the About Local Notifications and Remote Notifications, it does state:
Note: Remote notifications and local notifications are not related to broadcast notifications (NSNotificationCenter) or key-value observing notifications.
So, at that point, I'm going to answer from the perspective of NSDistributedNotificationCenter, and not about Remote/Local notifications - you already have a potential solution in the answer you linked for observing the DB file that contains a record of the notifications in that way.
This example code will not work on Catalina (10.15) because of API behavior changes
The first thing you need to do is listen for the correct notification. Creating a simple application that listens for all events; e.g. using:
NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
[center addObserver:self
selector:#selector(notificationEvent:)
name:nil
object:nil];
-(void)notificationEvent:(NSNotification *)notif {
NSLog(#"%#", notif);
}
It indicates that the notifications are:
__CFNotification 0x6100000464b0 {name = com.airserverapp.AudioDidStart; object = com.pratikkumar.airserver-mac; userInfo = {
ip = "2002:d55e:dbb2:1:10e0:1bfb:4e81:b481";
remote = YES;
}}
__CFNotification 0x618000042790 {name = com.airserverapp.AudioDidStop; object = com.pratikkumar.airserver-mac; userInfo = {
ip = "2002:d55e:dbb2:1:10e0:1bfb:4e81:b481";
remote = YES;
}}
This indicates that the name parameter in the addObserver call should be com.airserverapp.AudioDidStart and com.airserverapp.AudioDidStop.
You can use code like that to determine all notifications, which will allow you to add the relevant observers when you want the particular observer. This is probably the easiest way to observe notifications of this type.

NSNotificationCenter has a maximum speed?

Using NSNotificationCenter , it worked well till i have started to send very fast messages to other class.
What is fast ? its about 30-40 notifications in second. and i dont get even 1 of them .
is there other way to do that ?
should i update a global instead ?
//post data out .
- (void)post:(NSString*)string
{
NSLog(#"done"); //the log is printing
NSDictionary *userInfo = nil;
[[NSNotificationCenter defaultCenter] postNotificationName:#"connector"
object:string
userInfo:userInfo];
}
I know the observer is good cause same code worked before .
Thanks a lot .
If you're sending 30-40 notifications a second you should really think about your implementation again.
Heres some alternatives:
Creating your own delegates for callbacks
Using a block
The problem with NSNotificationCentre is that it sends the message to every observer - this can get slow and is generally used for updating views on state change (Login/Logout).
It looks like you setting the wrong object, Which should be the object that posts the notification (or nil). What I think you should be doing adding the string to userInfo:
NSDictionary *userInfo = #{#"somekey" : string };
[[NSNotificationCenter defaultCenter] postNotificationName:#"connector"
object:self
userInfo:userInfo];

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***********************");
}
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];
}
Your method takes one argument, so you should change
#selector(observerMethod)
to
#selector(observerMethod:)
If you're like me and having problems figuring out why in the world you are not getting receiving the NSWorkspaceWillPowerOffNotification notification, make sure you are setting yourself as an observer of [[NSWorkspace sharedWorkspace] notificationCenter] and -NOT- [NSNotificationCenter defaultCenter]!
I spent the better part of a day trying to debug why I wasn't getting that notification because I didn't read the answer clearly!
If you want to know when user resign or become active you just should subscribe to the following notifications:
Swift 3
NSWorkspace.shared().notificationCenter.addObserver(self, selector: #selector(sessionResignActive(_:)), name: NSNotification.Name.NSWorkspaceSessionDidResignActive, object: nil)
NSWorkspace.shared().notificationCenter.addObserver(self, selector: #selector(sessionBecomeActive(_:)), name: NSNotification.Name.NSWorkspaceSessionDidBecomeActive, object: nil)

Objective-C Mac OS X Distributed notifications iTunes

i need a little help, i currently have a method; updateTrackInfo in my Mac OS X application which gets the artist name, the track name and duration of the track currently being played in iTunes
However i want the app to listen for the distributed iTunes notification; com.apple.iTunes.playerInfo then call the method updateTrackInfo when ever the notification is distributed by iTunes. Please could someone help me, on what i would need to write in both the header and implementation file.
Thanks, Sami.
You're looking for -[NSDistributedNotificationCenter addObserver:selector:name:object:]:
NSDistributedNotificationCenter *dnc = [NSDistributedNotificationCenter defaultCenter];
[dnc addObserver:self selector:#selector(updateTrackInfo:) name:#"com.apple.iTunes.playerInfo" object:nil];
Elsewhere in the same class...
- (void) updateTrackInfo:(NSNotification *)notification {
NSDictionary *information = [notification userInfo];
NSLog(#"track information: %#", information);
}
It even gives you a whole bunch of track information in the notification. Isn't that nice?
Thanks for your help, you helped me correct my code, I had this written up:
- (id) init {
self = [super init];
if (!self) return nil;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(receiveNotification:)
name:#"com.apple.iTunes.playerInfo"
object:nil];
return self;}
- (void) receiveNotification:(NSNotification *) notification {
if ([#"com.apple.iTunes.playerInfo" isEqualToString:#"com.apple.iTunes.playerInfo"]) {
NSLog (#"Successfully received the test notification!");
}}
But it used NSNotificationCenter instead of NSDistributedNotificationCenter. Which is where I was going wrong.
Thanks, Sami.