Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I listen for all notifications sent to the iOS NSNotificationCenter's defaultCenter?

I want to listen to all notifications dispatched to the defaultCenter. Both public and private. Does anyone know how I can do this?

like image 960
Tony Avatar asked Oct 31 '11 14:10

Tony


People also ask

How do I use notification observer in Swift?

First, register an observer for a notification with: addObserver(_:selector:name:object:) Then, post a notification with post(name:object:userInfo:) … … after which your selector is called. And don't forget to remove the observer with removeObserver()

What is Nsnotificationcenter in iOS?

A notification dispatch mechanism that enables the broadcast of information to registered observers. iOS 2.0+ iPadOS 2.0+ macOS 10.0+ Mac Catalyst 13.0+ tvOS 9.0+ watchOS 2.0+


1 Answers

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

Example

The following code should do the job:

- (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]);                           }]; } 

Docs

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

addObserverForName:object:queue:usingBlock:

Adds an entry to the receiver’s dispatch table with a notification queue and a block to add to the queue, and optional criteria: notification name and sender.

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

Parameters

name

The name of the notification for which to register the observer; that is, only notifications with this name are used to add the block to the operation queue. If you pass nil, the notification center doesn’t use a notification’s name to decide whether to add the block to the operation queue.

obj

The object whose notifications you want to add the block to the operation queue. If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to add the block to the operation queue.

queue

The operation queue to which block should be added. If you pass nil, the block is run synchronously on the posting thread.

block

The block to be executed when the notification is received. The block is copied by the notification center and (the copy) held until the observer registration is removed. The block takes one argument:

notification

The notification.

like image 160
Sam Avatar answered Oct 11 '22 16:10

Sam