Observing all NSNotifications

×

This post was originally published in 2012
It may contain stale & outdated information. Or it may have grown more awesome with age, like the author.

When developing Rapid
Weaver plugins
, I find it it useful to add observers to some of the
notifications that Rapid Weaver throws around.

Before one can observe an NSNotification however, one must know it’s
called.

To discover this, one may observe all notifications, and log them:

- (void) init {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(globalHawk:)
                                                 name:nil
                                               object:nil];
}
- (void) globalHawk:(NSNotification *)notification {
    NSLog(@"%@", notification);
}

As you can imagine, this can result in a torrent of log entries. As one
likely knows some details about the notification one is searching for,
using the following template can make the list much more manageable:

- (void) globalHawk:(NSNotification *)notification { 
    if ([[notification name] rangeOfString:@"bestGuess"].location != NSNotFound) { 
        NSLog(@"%@", [notification name]); 
    } 
}

Have you ever had to sniff out an NSNotification from within an
application you don’t control?

No comments | Trackback