Apple Developer Connection
Advanced Search
Member Login Log In | Not a Member? Contact ADC

Technical Q&A QA1442
How to Observe All NSNotifications

Q: How can I observe all NSNotifications in my Cocoa application?

A: You can listen for all NSNotifications by adding yourself as an observer through three types of notification centers:

  • NSNotificationCenter

  • NSWorkspace

  • NSDistributedNotificationCenter

An NSNotificationCenter object provides a mechanism for broadcasting information within a task. It is essentially a notification dispatch table.

If you want to receive notifications from application requests such as launching of applications, opening, manipulating and obtaining information about files and devices, or tracking changes in the file system or user database, use NSWorkspace notification center.

If you want to receive notifications from other tasks, use NSDistributedNotificationCenter.

The following code listing shows how to accomplish this.

Listing 1: Observing all NSNotifications.

static BOOL g_observing;

- (void)startObserving
{
    if (!g_observing)
    {
        NSNotificationCenter* center;

        // 1) default notification center
        center = [NSNotificationCenter defaultCenter];
        [center addObserver: self
                selector: @selector(observeDefaultCenter:)
                name: nil
                object: nil];

        // 2) NSWorkspace notification center
        center = [[NSWorkspace sharedWorkspace] notificationCenter];
        [center addObserver: self
                selector: @selector(observeWorkspace:)
                name: nil
                object: nil];

        // 3) distributed notification center
        center = [NSDistributedNotificationCenter defaultCenter];
        [center addObserver: self
                selector: @selector(observeDistributed:)
                name: nil
                object: nil];

        g_observing = YES;
    }
}

- (void)stopObserving
{
    if (g_observing)
    {
        NSNotificationCenter* center;

        // remove notifications within our app
        center = [NSNotificationCenter defaultCenter];
        [center removeObserver: self];

        // remove notifications from NSWorkspace
        center = [[NSWorkspace sharedWorkspace] notificationCenter];
        [center removeObserver: self];

        // remove machine-wide notifications
        center = [NSDistributedNotificationCenter defaultCenter];
        [center removeObserver: self];

        g_observing = NO;
    }
}

- (void)observeDefaultCenter: (NSNotification*)notification
{
    NSLog(@"NSNotificationCenter: %@", [notification name]);
}

- (void)observeDistributed: (NSNotification*)notification
{
    NSLog(@"NSDistributedNotificationCenter: %@", [notification name]);
}

- (void)observeWorkspace: (NSNotification*)notification
{
    NSLog(@"NSWorkspace: %@", [notification name]);
}

WARNING: When deciding which or how many notifications you want to listen in your project, please consider the performance implications of listening to too many notifications.

For more information on NSNotifications, please refer to:

Document Revision History

DateNotes
2006-09-11First Version

Posted: 2006-09-11




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.