Guest User

CFNotificationCenter usage examples

a guest
Apr 1st, 2012
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. #include <CoreFoundation/CoreFoundation.h>
  2.  
  3. void notificationCallback (CFNotificationCenterRef center,
  4. void * observer,
  5. CFStringRef name,
  6. const void * object,
  7. CFDictionaryRef userInfo) {
  8. CFShow(CFSTR("Received notification (dictionary):"));
  9. // print out user info
  10. const void * keys;
  11. const void * values;
  12. CFDictionaryGetKeysAndValues(userInfo, &keys, &values);
  13. for (int i = 0; i < CFDictionaryGetCount(userInfo); i++) {
  14. const char * keyStr = CFStringGetCStringPtr((CFStringRef)&keys[i], CFStringGetSystemEncoding());
  15. const char * valStr = CFStringGetCStringPtr((CFStringRef)&values[i], CFStringGetSystemEncoding());
  16. printf("tt "%s" = "%s"n", keyStr, valStr);
  17. }
  18. }
  19.  
  20. int main (int argc, const char * argv[]) {
  21. CFNotificationCenterRef center = CFNotificationCenterGetLocalCenter();
  22. // add an observer
  23. CFNotificationCenterAddObserver(center, NULL, notificationCallback,
  24. CFSTR("MyNotification"), NULL,
  25. CFNotificationSuspensionBehaviorDeliverImmediately);
  26. // post a notification
  27. CFDictionaryKeyCallBacks keyCallbacks = {0, NULL, NULL, CFCopyDescription, CFEqual, NULL};
  28. CFDictionaryValueCallBacks valueCallbacks = {0, NULL, NULL, CFCopyDescription, CFEqual};
  29. CFMutableDictionaryRef dictionary = CFDictionaryCreateMutable(kCFAllocatorDefault, 1,
  30. &keyCallbacks, &valueCallbacks);
  31. CFDictionaryAddValue(dictionary, CFSTR("TestKey"), CFSTR("TestValue"));
  32. CFNotificationCenterPostNotification(center, CFSTR("MyNotification"), NULL, dictionary, TRUE);
  33. CFRelease(dictionary);
  34. // remove oberver
  35. CFNotificationCenterRemoveObserver(center, NULL, CFSTR("TestValue"), NULL);
  36. return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment