Difference between revisions of "CFNotificationCenter"

From iPhone Development Wiki
Jump to: navigation, search
(Add NSDate as property list object.)
(tiny copyedit)
Line 1: Line 1:
[[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.  
+
[[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 distributes them to the listeners.  
  
 
== Look up all observers of the local center ==
 
== Look up all observers of the local center ==
You can get the dictionary of all observers in the local center by
+
You can get the dictionary of all observers in the local center by doing this:
 
<source lang="c">
 
<source lang="c">
 
CFNotificationCenterRef center = CFNotificationCenterGetLocalCenter();
 
CFNotificationCenterRef center = CFNotificationCenterGetLocalCenter();
Line 10: Line 10:
  
 
== Post notification with objects ==
 
== Post notification with objects ==
DistributedCenter is silently available since iOS 5. It can send property list objects(NSArray, NSDictionary, NSNumber, NSString, NSDate and NSData) to other processes.
+
DistributedCenter has been silently available since iOS 5. It can send property list objects(NSArray, NSDictionary, NSNumber, NSString, NSDate and NSData) to other processes.
 
<source lang="objc">
 
<source lang="objc">
 
// First, define function as external.
 
// First, define function as external.
Line 37: Line 37:
 
     CFNotificationSuspensionBehaviorDeliverImmediately);
 
     CFNotificationSuspensionBehaviorDeliverImmediately);
 
</source>
 
</source>
You can check DistributedCenter can be used.
+
 
 +
You can check if DistributedCenter can be used:
 
<source lang="objc">
 
<source lang="objc">
 
void *handle = dlopen("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_LAZY);
 
void *handle = dlopen("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_LAZY);
Line 47: Line 48:
 
}
 
}
 
</source>
 
</source>
 +
 
== References ==
 
== References ==
 
* Official reference: http://developer.apple.com/iphone/library/documentation/CoreFoundation/Reference/CFNotificationCenterRef/Reference/reference.html
 
* Official reference: http://developer.apple.com/iphone/library/documentation/CoreFoundation/Reference/CFNotificationCenterRef/Reference/reference.html
  
 
{{occlass|library=CoreFoundation.framework|navbox=1}}
 
{{occlass|library=CoreFoundation.framework|navbox=1}}

Revision as of 22:14, 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 distributes them to the listeners.

Look up all observers of the local center

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

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

Post notification with objects

DistributedCenter has been silently available since iOS 5. It can send property list objects(NSArray, NSDictionary, NSNumber, NSString, NSDate 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);

You can check if DistributedCenter can be used:

void *handle = dlopen("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_LAZY);
void *impl = NULL;
if (handle)
    impl = dlsym(handle, "CFNotificationCenterGetDistributedCenter");
if (impl) {
    // Available.
}

References