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?
Related
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.
Through objective c coding. I want to get notified whenever a file is added to USB connected to my mac. I want to track all I/O operations done on it.Till now i am able to get notified whenever a USB is connected and Removed using
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector: #selector(volumesChanged:) name:NSWorkspaceDidMountNotification object: nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector: #selector(volumesRemoved:) name:NSWorkspaceDidUnmountNotification object:nil];
Similarly want to know whenever any operations are done on it. Any help is appreciable. Thanks in advance
You can use FSEvents for monitoring file system changes. This is the documentation - https://developer.apple.com/library/mac/documentation/Darwin/Reference/FSEvents_Ref/
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.
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.
I am looking for a way to execute my app (it's a background task) at times, where the machine is "idle". A good incident would be when the screensaver launches. I already read the manual auf launchd and already use a LaunchAgent to lauch my app at certain intervals, but I found nothing that might help me launching my app when the screensaver is active.
Is there any possibility to do that?
Thanks in advance!
Josh
Have another process that runs in the background and listens for distributed notifications named com.apple.screenIsLocked and com.apple.screenIsUnlocked. (That's for Snow Leopard. Leopard used different notification names. Use Notification Watcher and experimentation to find out what they are.) When one of those notifications comes in, launch or nicely-quit* your real app, as appropriate.
*You'll want to use an Apple Event for this.
For Snow Leopard the screenIsLocked and screenIsUnlocked notifications aren't available anymore. What I've used successfully are these:
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:#selector(locked:) name:NSWorkspaceScreensDidSleepNotification object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:#selector(unlocked:) name:NSWorkspaceScreensDidWakeNotification object:nil];
I'm a new user, so I can't comment on or vote up portenkirchner's suggestion. Matt Swann has moved his ScriptSaver.
It does exactly what I want, run an AppleScript program when I unlock the screensaver.
You can use ScriptSaver by Matt Swann. This is a Mac OS X screensaver which can run AppleScripts when it activates and deactivates.
http://swannman.wordpress.com/projects/scriptsaver/