NSNotification and NSNotificationCenter Tutorial

In NSNotification Tutorial, I have explained how to send data from one class to other using NSNotificationCenter in iOS.
Note: Using NSNotificationCenter we can send the data only with in the app. we can not send data to other apps.

NSNotificationCenter Example Source

1) How to Register for notifications

NSNotificationCenter has the following observer method to register for receiving notifications.

- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;

 

Example code to register notifications with name “NOTIFICATION_1”, “NOTIFICATION_2”

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(receivedNotification:)
                                                 name:@"NOTIFICATION_1" object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(receivedNotification:)
                                                 name:@"NOTIFICATION_2" object:nil];

receivedNotification method is called when there is notification with name “NOTIFICATION_1” or “NOTIFICATION_2”

-(void) receivedNotification:(NSNotification*) notification
{
	//name of the notification
    NSString * name =notification.name;

    //notification userinfo
    NSDictionary * info =notification.userInfo;

    NSLog(@"Received Notification with name =%@",name);
    NSLog(@"Information =%@",info);

}

2). How to Send Notifications

To send notification, we need to prepare a NSNotification object. Then using NSNotificationCenter it can be broadcasted.

2.1) Preparing NSNotification object
NSNotification has the following methods to prepare an object.

+ (instancetype)notificationWithName:(NSString *)aName object:(id)anObject;
+ (instancetype)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;
- (instancetype)initWithName:(NSString *)name object:(id)object userInfo:(NSDictionary *)userInfo NS_AVAILABLE(10_6, 4_0);

Below code explains how to prepare NSNotification object.

NSDictionary * dict =[NSDictionary dictionaryWithObject:@"Ravi" forKey:@"name"];
NSNotification * notification =[[ NSNotification alloc]
                                    initWithName:@"NOTIFICATION_2" object:nil userInfo:dict];

2.2 Post Notification
NSNotificationCenter has the following methods to post notification.

- (void)postNotification:(NSNotification *)notification;
- (void)postNotificationName:(NSString *)aName object:(id)anObject;
- (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;

Example code for posting notifications.

	NSString * noticationName =@"HELLO";

    NSDictionary * dict =[NSDictionary dictionaryWithObject:@"Ravi" forKey:@"name"];
    NSNotification * notification =[[ NSNotification alloc]
                                    initWithName:noticationName object:nil userInfo:dict];

    //EX 1: with NSNotification object
    [[NSNotificationCenter defaultCenter] postNotification:notification];

    //EX 2: without Userinfo and without preparing NSNotification object
    [[NSNotificationCenter defaultCenter] postNotificationName:noticationName object:nil];

    //EX 3: without preparing NSNotification Object
    [[NSNotificationCenter defaultCenter] postNotificationName:noticationName
                                                        object:nil userInfo:dict];

 

3) How to Unregister Notification

NSNotificationCenter has the following the methods to unregister Notifications.

- (void)removeObserver:(id)observer;
- (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;

Example code to unregister notifications:

    NSString * noticationName =@"HELLO";

    //EX 1: unreigster all the notifications in the object.
    [[NSNotificationCenter defaultCenter] removeObserver:self];

    //EX 2: unregister specific notification
    [[NSNotificationCenter defaultCenter] removeObserver:self name:noticationName object:nil];

Reference: Apple Documentation