Skip to content

Instantly share code, notes, and snippets.

@mmackh
Last active December 23, 2015 09:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmackh/6616295 to your computer and use it in GitHub Desktop.
Save mmackh/6616295 to your computer and use it in GitHub Desktop.
NSObject category for an NSNotificationCenter abstraction to keep your code clean & lean.
//
// NSObject+Notifications.h
// InstaPDF
//
// Created by Maximilian Mackh on 18/09/13.
// Copyright (c) 2013 mackh ag. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSObject (Notifications)
/// Subscribe to multiple NSNotifications
- (void)subscribeToNotifications:(NSArray *)notifications;
/// Unsubscribe from multiple NSNotifications
- (void)unsubscribeFromNotifications:(NSArray *)notifications;
/// Convenience method to subscribe to a single NSNotifications
- (void)subscribeToNotification:(NSString *)notificationName;
/// Convenience method to unsubscribe from a single NSNotifications
- (void)unsubscribeFromNotification:(NSString *)notificationName;
/// Unsubscribe from all NSNotifications. Seriously, unsubscribe from all notification to keep NSNotificationCenter clean
- (void)unsubscribeFromAllNotifications;
/// Post a NSNotification
- (void)postNotificationWithName:(NSString *)notificationName;
/// Post a NSNotification with an Object
- (void)postNotificationWithName:(NSString *)notificationName object:(id)notificationObject;
/// REQUIRED: When subscribing to any NSNotification, implement this method. Object can be nil
- (void)receivedNotificationWithName:(NSString *)notificationName object:(id)notificationObject;
// Begin logging all NSNotifications
- (void)beginNotificationLogging;
// End logging all NSNotifications
- (void)endNotificationLogging;
@end
//
// NSObject+Notifications.m
// InstaPDF
//
// Created by Maximilian Mackh on 18/09/13.
// Copyright (c) 2013 mackh ag. All rights reserved.
//
#import "NSObject+Notifications.h"
#pragma clang diagnostic ignored "-Wincomplete-implementation"
@implementation NSObject (Notifications)
- (void)subscribeToNotifications:(NSArray *)notifications
{
for (id notification in notifications)
{
if ([notification isKindOfClass:[NSString class]])
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_notf_received_ipdf:) name:notification object:nil];
}
}
}
- (void)unsubscribeFromNotifications:(NSArray *)notifications
{
for (id notification in notifications)
{
if ([notification isKindOfClass:[NSString class]])
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:notification object:nil];
}
}
}
- (void)subscribeToNotification:(NSString *)notificationName
{
[self subscribeToNotifications:@[notificationName]];
}
- (void)unsubscribeFromNotification:(NSString *)notificationName
{
[self unsubscribeFromNotifications:@[notificationName]];
}
- (void)unsubscribeFromAllNotifications
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)postNotificationWithName:(NSString *)notificationName
{
[[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:nil];
}
- (void)postNotificationWithName:(NSString *)notificationName object:(id)notificationObject
{
[[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:notificationObject];
}
- (void)_notf_received_ipdf:(NSNotification *)notification
{
[self performSelector:@selector(receivedNotificationWithName:object:) withObject:notification.name withObject:notification.object];
}
- (void)beginNotificationLogging
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_notf_log_ipdf:) name:nil object:nil];
}
- (void)endNotificationLogging
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:nil];
}
- (void)_notf_log_ipdf:(NSNotification *)notification
{
NSLog(@"%@",notification);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment