How to retrieve all NSNotificationCenter observers?

Ortwin Gentz picture Ortwin Gentz · Nov 7, 2012 · Viewed 7k times · Source

I'd like to retrieve a list of observers (objects and selectors) for a given notification name. I know there's no official API for that. I also know I could subclass NSNotificationCenter to accomplish this. Sometimes however this is not a viable option because NSNotificationCenter usage is spread all over the code or even binary frameworks.

So I'm looking for an unofficial/private way to do this. (Since it's about debugging only, that's fine.)

Answer

Ortwin Gentz picture Ortwin Gentz · Nov 25, 2015

Finally, Apple added a way to print all notification center observers:

po [NSNotificationCenter defaultCenter]

It prints a comma separated list with Name, Object, Observer, and Options:

<NSNotificationCenter:0x7f997b307500>
Name, Object, Observer, Options
WebPreferencesRemovedNotification, 0x11165b680, 0x116c87ff8, 1400
UIApplicationWillEnterForegroundNotification, 0x11165b680, 0x7f997a838000, 1400
...