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.)
- Unit Testing of private methods in Xcode
- How can you implement the NSDocument method -canCloseDocumentWithDelegate:shouldCloseSelector:contextInfo: in Swift?
- sound picker / list of system sounds
- What are the new “iOS data protection APIs”?
- Stop / Pause swift app for period of time
- what is diff. b/w @property (nonatomic,assign) and @property (nonatomic,retain)
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
- xcode A device with number already exists on this team.
- How to implement fast image filters on iOS platform
- How to change UIButton image after clicking in Swift?
- returning a value from a function with Alamofire and SwiftyJson
- App Store submission/distribution error “failed to open ssh session”
- Cocoapods: turning MagicalRecord logging off
- Airplay on new Apple TV (3rd gen.) for second screen not at 1080p
- Detect AppStore installation of iOS app
- How to dismiss a modal VC with fade out animation?
- Swift: How to multiply array by array (Math: vector by vector)
- Disabling a specific warning in a specific line in Xcode
- Setting Multiple Times for Notifications in Swift
- Enable a button in Swift only if all text fields have been filled out
- Xcode: Run project with specified localization
- Connecting iOS with Azure Cloud