Difference between revisions of "CFNotificationCenter"

From iPhone Development Wiki
Jump to: navigation, search
m (Created page with 'CFNotificationCenter is an object representing a local or remote ''notification center'', which is a singleton object of its type that receives ''notifications'' from differe…')
 
(Add post notification with property list objects code.)
Line 7: Line 7:
 
CFDictionaryRef dict = *(CFDictionaryRef*)(center + 4);
 
CFDictionaryRef dict = *(CFDictionaryRef*)(center + 4);
 
CFShow(dict);
 
CFShow(dict);
 +
</source>
 +
 +
== Post notification with objects ==
 +
DistributedCenter is silently available since iOS 5. It can send property list objects(NSArray, NSDictionary, NSNumber, NSString and NSData) to other processes.
 +
<source lang="objc">
 +
// First, define function as external.
 +
extern CFNotificationCenterRef CFNotificationCenterGetDistributedCenter(void);
 +
 +
// Sender.
 +
CFMutableDictionaryRef dictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
 +
CFDictionaryAddValue(dictionary, @"key of string", @1);
 +
CFNotificationCenterPostNotification(CFNotificationCenterGetDistributedCenter(), CFSTR("notification.identifier"), @"string object", dictionary, true);
 +
CFRelease(dictionary);
 +
 +
...
 +
 +
// Call back function.
 +
static void CallBackFunction(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
 +
{
 +
    ...
 +
}
 +
 +
// Registration in receiver process.
 +
CFNotificationCenterAddObserver(CFNotificationCenterGetDistributedCenter(),
 +
    NULL,
 +
    CallBackFunction,
 +
    CFSTR("notification.identifier"),
 +
    NULL,
 +
    CFNotificationSuspensionBehaviorDeliverImmediately);
 
</source>
 
</source>
  

Revision as of 15:23, 4 March 2014

CFNotificationCenter is an object representing a local or remote notification center, which is a singleton object of its type that receives notifications from different sources, and distribute to the listeners.

Look up all observers of the local center

You can get the dictionary of all observers in the local center by

CFNotificationCenterRef center = CFNotificationCenterGetLocalCenter();
CFDictionaryRef dict = *(CFDictionaryRef*)(center + 4);
CFShow(dict);

Post notification with objects

DistributedCenter is silently available since iOS 5. It can send property list objects(NSArray, NSDictionary, NSNumber, NSString and NSData) to other processes.

// First, define function as external.
extern CFNotificationCenterRef CFNotificationCenterGetDistributedCenter(void);

// Sender.
CFMutableDictionaryRef dictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionaryAddValue(dictionary, @"key of string", @1);
CFNotificationCenterPostNotification(CFNotificationCenterGetDistributedCenter(), CFSTR("notification.identifier"), @"string object", dictionary, true);
CFRelease(dictionary);

...

// Call back function.
static void CallBackFunction(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
    ...
}

// Registration in receiver process.
CFNotificationCenterAddObserver(CFNotificationCenterGetDistributedCenter(),
    NULL,
    CallBackFunction,
    CFSTR("notification.identifier"),
    NULL,
    CFNotificationSuspensionBehaviorDeliverImmediately);

References