Global Notifications in iOS

So this is how I created some global Notifications for my application. I'm not sure if this is the best way but it works. The benefit is that you don't have to add an import statement in every single place that needs to access the Notification constants.

Step 1: Create Constants.h

Create a header file that will contain the global constants that will be accessed by your application.

extern NSString *const BZRoomDidClearBadgeNotification;  
extern NSString *const BZRoomDidClearBadgeRoomKey;  
extern NSString *const BZRoomDidClearBadgeRoomNameKey;  

Step 2: Create the Constants.m

Create the implementation file Constants.m. This file will implement your constants. In this case, because I'm just creating Notifications, I create a string that corresponds to the name of the constant.

Create the implementation file:

NSString *const BZRoomDidClearBadgeNotification = @"BZRoomDidClearBadgeNotification";  
NSString *const BZRoomDidClearBadgeRoomKey = @"BZRoomDidClearBadgeRoomKey";  

Step 3: Add Constants.h to Prefix.pch

Finally, add the header reference to the Prefix.pch so that it is globally included.

//  Prefix header
//  The contents of this file are implicitly included at the beginning of every source file.

#import <Availability.h>

#ifndef __IPHONE_3_0
#warning "This project uses features only available in iOS SDK 3.0 and later."

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import "Constants.h"
comments powered by Disqus