2011-08-06 3 views
4

저는 아직도 이것에 익숙하지 않지만 저는 예제를 통해 정말 빨리 배웠습니다. 현재 실행중인 프로그램에서 다른 프로그램으로 알림을 게시하려고합니다. CFNotificationCenter가 앞으로의 길입니다. 유일한 문제는 그것을 사용하기 위해 운동 할 수 없으며 apple의 videoviewer를 제외하고는 어떤 사례도없는 것처럼 보입니다.CFNotificationCenter 사용 예?

아무에게도 알림을 게시하는 하나의 응용 프로그램을 작성할 수 있도록 테스트 알림 및 doSomething();을받을 수 있도록 설정하는 방법에 대한 간단한 예제를 제공 할 수 있습니까? 어떤 도움이라도 대단히 감사합니다!

+1

CFNotificationCenter 또는 NSNotificationCenter 사용에 대한 예가 필요합니까? CFNotificationCenter는 NSNotificationCenter와 동일한 CoreFoundation입니다. 그들은 똑같은 일을합니다. Stackoverflow에 NSNotificationCenter의 [예제] (http://stackoverflow.com/questions/2191594/how-to-send-and-receive-message-through-nsnotificationcenter-in-objective-c)가 도움이 될 수 있습니다. 아웃. –

답변

9

좋아, 나는 CFNotificationCenter의 작은 예제를 썼다. 일반적으로 대규모 프로젝트에는 CoreFoundation을 사용하지 않고 대신 Foundation을 사용합니다. Objective-C에서이 프로젝트를 실제로 작성하는 경우 (내가 태그에서 추측 한대로) NSNotificationCenter을 사용하는 것이 좋습니다.

#include <CoreFoundation/CoreFoundation.h> 

void notificationCallback (CFNotificationCenterRef center, 
          void * observer, 
          CFStringRef name, 
          const void * object, 
          CFDictionaryRef userInfo) { 
    CFShow(CFSTR("Received notification (dictionary):")); 
    // print out user info 
    const void * keys; 
    const void * values; 
    CFDictionaryGetKeysAndValues(userInfo, &keys, &values); 
    for (int i = 0; i < CFDictionaryGetCount(userInfo); i++) { 
     const char * keyStr = CFStringGetCStringPtr((CFStringRef)&keys[i], CFStringGetSystemEncoding()); 
     const char * valStr = CFStringGetCStringPtr((CFStringRef)&values[i], CFStringGetSystemEncoding()); 
     printf("\t\t \"%s\" = \"%s\"\n", keyStr, valStr); 
    } 
} 

int main (int argc, const char * argv[]) { 
    CFNotificationCenterRef center = CFNotificationCenterGetLocalCenter(); 
    // add an observer 
    CFNotificationCenterAddObserver(center, NULL, notificationCallback, 
            CFSTR("MyNotification"), NULL, 
            CFNotificationSuspensionBehaviorDeliverImmediately); 
    // post a notification 
    CFDictionaryKeyCallBacks keyCallbacks = {0, NULL, NULL, CFCopyDescription, CFEqual, NULL}; 
    CFDictionaryValueCallBacks valueCallbacks = {0, NULL, NULL, CFCopyDescription, CFEqual}; 
    CFMutableDictionaryRef dictionary = CFDictionaryCreateMutable(kCFAllocatorDefault, 1, 
                    &keyCallbacks, &valueCallbacks); 
    CFDictionaryAddValue(dictionary, CFSTR("TestKey"), CFSTR("TestValue")); 
    CFNotificationCenterPostNotification(center, CFSTR("MyNotification"), NULL, dictionary, TRUE); 
    CFRelease(dictionary); 
    // remove oberver 
    CFNotificationCenterRemoveObserver(center, NULL, CFSTR("TestValue"), NULL); 
    return 0; 
} 

이 예는 관찰자, 그것에 게시물 간단한 사전을 생성하고, 관찰자를 제거합니다 : 더 adue없이, 여기에 예입니다. CFNotificationCenter에 대한 자세한 내용은 Apple's CFNotificationCenter Reference에서 확인할 수 있습니다.

+0

고마워요,이 조각들은 내가 찾고 있던 것이고 지금은 그것이 어떻게 작동하는지 이해합니다. 두 가지 응용 프로그램 사이에서이 작업을 수행하므로 로컬 응용 프로그램 대신 CFNotificationCenterGetDistributedCenter를 사용했습니다. 하지만 그렇지 않으면 고마워! – Tiago

+0

이것을 XCode 7.1로 잘라내거나 붙여 넣을 때, 컴파일되지 않고 "notificationCallback (')의 & keys [i]와'& values ​​[i]'에"불완전한 타입의 포인터의 첨자')'함수를 호출합니다. – Volomike