View Full Version : Initializing an app

12-31-2010, 11:29 AM
Hey guys, I have a certain conundrum to figure out here.

I'm writing a 'service' app of sorts. Basically, it subscribes to [[NSWorkspace sharedWorkspace] notificationCenter] to monitor events. The problem is, I can't figure out how to make it subscribe to certain notifications, efficiently.

The idea I've had so far is to use the AppDelegate's applicationDidFinishLaunching: message to set it up. I'd have an array of strings in [NSUserDefaults standardUserDefaults] that represent desired notifications. I'd let a helper object (NotificationHandler) that's part of AppDelegate be subscribed to these notifications. Here I'm having trouble figuring out how to add methods to the NotificationHandler and how to subscribe.

Any ideas?

Thanks in advance!

12-31-2010, 11:40 AM
OK, I think I got something better. Something like this:

NSNotificationCenter * workspaceNotification = [[NSWorkspace sharedWorkspace] addObserver:notificationHandler selector:@selector(handleNotification:) name:nil object:nil];

In the NotificationHandler, I check whether the notification is in the sharedUserDefaults:

- (void)handleNotification:(NSNotification *)note {
NSString * noteName = [note name];
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"SubscribedNotes"] containsObject:noteName]) {
// handle notification appropriately, I have code for this

Should this work? Is this a good idea?