iOS Developer Library

Developer

Foundation Framework Reference NSNotificationCenter Class Reference

Options
Deployment Target:

On This Page
Language:

NSNotificationCenter

Inheritance


Conforms To


Import Statement


Swift

import Foundation

Objective-C

@import Foundation;

Availability


Available in iOS 2.0 and later.

An NSNotificationCenter object (or simply, notification center) provides a mechanism for broadcasting information within a program. An NSNotificationCenter object is essentially a notification dispatch table.

Objects register with a notification center to receive notifications (NSNotification objects) using the addObserver:selector:name:object: or addObserverForName:object:queue:usingBlock: methods. Each invocation of this method specifies a set of notifications. Therefore, objects may register as observers of different notification sets by calling these methods several times.

Each running Cocoa program has a default notification center. You typically don’t create your own. An NSNotificationCenter object can deliver notifications only within a single program. If you want to post a notification to other processes or receive notifications from other processes, use an instance of NSDistributedNotificationCenter.

  • Returns the process’s default notification center.

    Declaration

    Swift

    class func defaultCenter() -> NSNotificationCenter

    Objective-C

    + (NSNotificationCenter *)defaultCenter

    Return Value

    The current process’s default notification center, which is used for system notifications.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • 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.

    Declaration

    Swift

    func addObserverForName(_ name: String?, object obj: AnyObject?, queue queue: NSOperationQueue?, usingBlock block: (NSNotification!) -> Void) -> NSObjectProtocol

    Objective-C

    - (id<NSObject>)addObserverForName:(NSString *)name object:(id)obj queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))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.

    Return Value

    An opaque object to act as the observer.

    Discussion

    If a given notification triggers more than one observer block, the blocks may all be executed concurrently with respect to one another (but on their given queue or on the current thread).

    The following example shows how you can register to receive locale change notifications.

    • NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    • NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
    • self.localeChangeObserver = [center addObserverForName:NSCurrentLocaleDidChangeNotification object:nil
    • queue:mainQueue usingBlock:^(NSNotification *note) {
    • NSLog(@"The user's locale changed to: %@", [[NSLocale currentLocale] localeIdentifier]);
    • }];

    To unregister observations, you pass the object returned by this method to removeObserver:. You must invoke removeObserver: or removeObserver:name:object: before any object specified by addObserverForName:object:queue:usingBlock: is deallocated.

    • NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    • [center removeObserver:self.localeChangeObserver];

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 4.0 and later.

  • Adds an entry to the receiver’s dispatch table with an observer, a notification selector and optional criteria: notification name and sender.

    Declaration

    Swift

    func addObserver(_ notificationObserver: AnyObject, selector notificationSelector: Selector, name notificationName: String?, object notificationSender: AnyObject?)

    Objective-C

    - (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender

    Parameters

    notificationObserver

    Object registering as an observer. This value must not be nil.

    notificationSelector

    Selector that specifies the message the receiver sends notificationObserver to notify it of the notification posting. The method specified by notificationSelector must have one and only one argument (an instance of NSNotification).

    notificationName

    The name of the notification for which to register the observer; that is, only notifications with this name are delivered to the observer.

    If you pass nil, the notification center doesn’t use a notification’s name to decide whether to deliver it to the observer.

    notificationSender

    The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the observer.

    If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.

    Discussion

    Be sure to invoke removeObserver:name:object: before notificationObserver or any object specified in addObserver:selector:name:object: is deallocated.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • Removes all the entries specifying a given observer from the receiver’s dispatch table.

    Declaration

    Swift

    func removeObserver(_ notificationObserver: AnyObject)

    Objective-C

    - (void)removeObserver:(id)notificationObserver

    Parameters

    notificationObserver

    The observer to remove. Must not be nil.

    Discussion

    Be sure to invoke this method (or removeObserver:name:object:) before notificationObserver or any object specified in addObserver:selector:name:object: is deallocated.

    You should not use this method to remove all observers from an object that is going to be long-lived, because your code may not be the only code adding observers that involve the object.

    The following example illustrates how to unregister someObserver for all notifications for which it had previously registered. This is safe to do in the dealloc method, but should not otherwise be used—use removeObserver:name:object: instead.

    • [[NSNotificationCenter defaultCenter] removeObserver:someObserver];

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • Removes matching entries from the receiver’s dispatch table.

    Declaration

    Swift

    func removeObserver(_ notificationObserver: AnyObject, name notificationName: String?, object notificationSender: AnyObject?)

    Objective-C

    - (void)removeObserver:(id)notificationObserver name:(NSString *)notificationName object:(id)notificationSender

    Parameters

    notificationObserver

    Observer to remove from the dispatch table. Specify an observer to remove only entries for this observer. Must not be nil, or message will have no effect.

    notificationName

    Name of the notification to remove from dispatch table. Specify a notification name to remove only entries that specify this notification name. When nil, the receiver does not use notification names as criteria for removal.

    notificationSender

    Sender to remove from the dispatch table. Specify a notification sender to remove only entries that specify this sender. When nil, the receiver does not use notification senders as criteria for removal.

    Discussion

    Be sure to invoke this method (or removeObserver:) before the observer object or any object specified in addObserver:selector:name:object: is deallocated.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.