Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/html/extensions/Variables/includes/ExtVariables.php on line 198
CFNotificationCenter: Difference between revisions - iPhone Development Wiki

CFNotificationCenter: Difference between revisions

From iPhone Development Wiki
(Added info on registering for CFNotificationCenter callbacks)
m (Reordering)
 
Line 3: Line 3:
== Great way to determine various PRIVATE events that may want to subscribe to ==
== Great way to determine various PRIVATE events that may want to subscribe to ==


Add the observer below in your app startup code and implement the callback handler below. Then perform actions on your phone or other apps and log the events you may be interested in.
Add the observer below in your startup code and implement the callback handler below. Then perform actions on your phone or other apps and log the events you may be interested in.
 
<source lang="c">
<source lang="c">
// Make the callback handler
void notificationCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
NSLog(@"Callback detected: \n\t name: %@ \n\t object:%@", name, object);
}
void startupFunction() {
    ...
    // Register your handler
     CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(),
     CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(),
                                     NULL,
                                     NULL,
Line 11: Line 20:
                                     NULL,   
                                     NULL,   
                                     CFNotificationSuspensionBehaviorDeliverImmediately);
                                     CFNotificationSuspensionBehaviorDeliverImmediately);
</source>
    ...
 
And the callback:
<source lang="c">
void notificationCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
{
    NSLog(@"Callback detected: \n\t name: %@ \n\t object:%@", name, object);
}
}
</source>
</source>
Line 24: Line 27:


== 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 doing this:
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 32: Line 37:


== Post notification with objects ==
== 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.
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.
extern CFNotificationCenterRef CFNotificationCenterGetDistributedCenter(void);
extern CFNotificationCenterRef CFNotificationCenterGetDistributedCenter(void);


// Sender.
// Implement a callback function.
CFMutableDictionaryRef dictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
static void CallBackFunction(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
CFDictionaryAddValue(dictionary, @"key of string", @1);
...
CFNotificationCenterPostNotification(CFNotificationCenterGetDistributedCenter(), CFSTR("notification.identifier"), @"string object", dictionary, true);
NSLog(@"Callback detected: \n\t name: %@ \n\t object:%@", name, object);
CFRelease(dictionary);
}


...
...


// Call back function.
void startup() {
static void CallBackFunction(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo)
...
{
// Check if DistributedCenter is available:
    ...
void *handle = dlopen("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_LAZY);
void *impl = NULL;
if (handle) {
impl = dlsym(handle, "CFNotificationCenterGetDistributedCenter");
}
if (impl) { // Available.
// Registration in receiver process.
CFNotificationCenterAddObserver(CFNotificationCenterGetDistributedCenter(),
NULL,
CallBackFunction,
CFSTR("notification.identifier"),
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
}
}
...
}
}


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


You can check if DistributedCenter can be used:
// Sender
<source lang="objc">
void somewhereElse() {
void *handle = dlopen("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_LAZY);
...
void *impl = NULL;
CFMutableDictionaryRef dictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
if (handle)
CFDictionaryAddValue(dictionary, @"key of string", @1);
    impl = dlsym(handle, "CFNotificationCenterGetDistributedCenter");
CFNotificationCenterPostNotification(CFNotificationCenterGetDistributedCenter(), CFSTR("notification.identifier"), @"string object", dictionary, true);
if (impl) {
CFRelease(dictionary);
    // Available.
...
}
}
</source>
</source>


== References ==
== External links ==
 
* 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}}

Latest revision as of 06:15, 26 October 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.

Great way to determine various PRIVATE events that may want to subscribe to

Add the observer below in your startup code and implement the callback handler below. Then perform actions on your phone or other apps and log the events you may be interested in.

// Make the callback handler
void notificationCallback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
	NSLog(@"Callback detected: \n\t name: %@ \n\t object:%@", name, object);
}

void startupFunction() {
    ...
    // Register your handler
    CFNotificationCenterAddObserver(CFNotificationCenterGetLocalCenter(),
                                    NULL,
                                    notificationCallback,
                                    NULL,
                                    NULL,  
                                    CFNotificationSuspensionBehaviorDeliverImmediately);
    ...
}

Hat tip: http://stackoverflow.com/questions/3725234/nsnotificationcenter-trapping-and-tracing-all-nsnotifications/3738387#3738387

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);

// Implement a callback function.
static void CallBackFunction(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
	...
	NSLog(@"Callback detected: \n\t name: %@ \n\t object:%@", name, object);
}

...

void startup() {
	...
	// Check if DistributedCenter is available:
	void *handle = dlopen("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_LAZY);
	void *impl = NULL;
	if (handle) {
		impl = dlsym(handle, "CFNotificationCenterGetDistributedCenter");
	}
	if (impl) { // Available.
		// Registration in receiver process.
		CFNotificationCenterAddObserver(CFNotificationCenterGetDistributedCenter(),
			NULL,
			CallBackFunction,
			CFSTR("notification.identifier"),
			NULL,
			CFNotificationSuspensionBehaviorDeliverImmediately);
		}
	}
	...
}

...

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

External links