How to retrieve all NSNotificationCenter observers?
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.)
2 Solutions Collect From Internet About “How to retrieve all NSNotificationCenter observers?”
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 ...
If you don’t want to subclass
NSNotificationCenter you can rename original
addObserver:selector:name:object method and create your own with such name and add observers in there to some array then call original renamed method.
Take a look at following methods:
Also look at this SO question: Method Swizzling
I am not sure why you want observers but you might find this class useful, which removes observers automatically for you which I think might be what you want. SFObservers
- Lion 10.7.3: Apple is not currently accepting applications built with this version of the OS
- Syncing only one entity in Core Data to iCloud
- How to Autorelease a CGImageRef?
- iOS : Objective-C : Firebase : Is it possible to read the cached data first for any reference?
- iPhone iOS how to redraw UINavigationBar on demand?
- Better image coloring logic/algorithm
- Image Array IBAction to view next image in the array
- How to add followed text to uitextfield
- How to add a dropdown menu to the Navigation Bar Title in iOS
- How to add Entitlement.plist in xCode 4.2
- IOS: one account two iOS developer program
- UILabel clipping italic (oblique) text at left and right edges of content ( iOS 6+)
- Why retain count in negative value?
- 'Cordova/CDVViewController.h' file not found when run in Xcode terminal