How can I listen to all notifications sent to iOS NSNotificationCenter defaultCenter?

I want to listen to all notifications sent to the center by default. Both public and private. Does anyone know how I can do this?

+42
ios nsnotificationcenter
Oct 31 '11 at 14:14
source share
1 answer

Use the NSNotificationCenter addObserverForName:object:queue:usingBlock: OR the addObserver:selector:name:object: method and pass nil for the name and object.

Example

The following code should complete the task:

 - (void)dumpNotifications { NSNotificationCenter *notifyCenter = [NSNotificationCenter defaultCenter]; [notifyCenter addObserverForName:nil object:nil queue:nil usingBlock:^(NSNotification *notification){ // Explore notification NSLog(@"Notification found with:" "\r\n name: %@" "\r\n object: %@" "\r\n userInfo: %@", [notification name], [notification object], [notification userInfo]); }]; } 

Documents

Here are the docs on addObserverForName:object:queue:usingBlock: In particular, see Name Parameters and obj.

addObserverForName: Object: Queues: usingBlock:

Adds an entry to the recipient sending table with the notification queue and a block for adding to the queue, as well as additional criteria: notification name and sender.

- (id)addObserverForName:(NSString *)name object:(id)obj queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *))block

Options

name

The name of the notification for which the observer is being registered; that is, only notifications with this name are used to add a block to the operation queue. If you pass zero, the notification center does not use the notification name to decide whether to add a block to the operation queue.

Obj

The object whose notifications you want to add to the operation queue block. If you pass zero, the notification center does not use the notification sender to decide whether to add a block to the operation queue.

queues

The operational queue to which the block should be added. If you pass zero, the block runs synchronously in the posting flow.

block

The block that will be executed when a notification is received. The block is copied by the notification center and (copy) stored until the observer registration is deleted. The block takes one argument:

notification

Notification.

+70
Oct 31 2018-11-11T00:
source share
โ€” -



All Articles