Core Foundation DEMO: Tweak端:
- CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
- NULL,
- &NotificationReceivedCallback,
- CFSTR("com.chinapyg.fakecarrier-change"),
- NULL,
- CFNotificationSuspensionBehaviorCoalesce);
- 回調:
- static void NotificationReceivedCallback(CFNotificationCenterRef center,
- void *observer, CFStringRef name,
- const void *object, CFDictionaryRef
- userInfo)
- {
- //.... 可以根據 name來判斷是何種消息,下面的客戶端傳了NULL,所以無需判斷了,在多種消息的時候需要用到
- }
復制代碼
APP端: 1.一句代碼即可
- notify_post("com.chinapyg.fakecarrier-change");
復制代碼
2.復雜點的
- CFStringRef observedObject =
- CFSTR("com.chinapyg.fakecarrier-change");
- CFNotificationCenterRef center =
- CFNotificationCenterGetDistributedCenter();
- CFNotificationCenterPostNotification(center, NULL,
- observedObject, NULL /* no dictionary */, TRUE);
復制代碼
/////////////////////////////////////////////////////////////////////////////////////////// 華麗的分割線 /////////////////////////////////////////////////////////////////////////////////////////// Cocoa DEMO:
接收端(后台):
- NSString *observedObject = @"com.chinapyg.notification";
- // 處理單個計算機上不同的進程之間的通知
- NSDistributedNotificationCenter *center =
- [NSDistributedNotificationCenter defaultCenter];
- [center addObserver: self
- selector: @selector(callbackWithNotification:)
- name: @"PiaoYun Notification"
- object: observedObject];
- 回調:
- - (void)callbackWithNotification:(NSNotification *)myNotification;
- {
- NSLog(@"Notification Received");
- }
復制代碼
發送端(app):
- NSString *observedObject = @"com.mycompany.notification";
- NSDistributedNotificationCenter *center =
- [NSDistributedNotificationCenter defaultCenter];
- [center postNotificationName: @"PiaoYun Notification"
- object: observedObject
- userInfo: nil /* no dictionary */
- deliverImmediately: YES];
復制代碼
iOS上層接口:
- // 處理單進程之間的通知
- [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(callBack) name: @"back" object: nil];
- // 回調
- - (void)callBack
- {
- NSLog(@"Notification Received");
- }
- //發出通知
- [[NSNotificationCenter defaultCenter] postNotificationName:@"back" object:self];
復制代碼
|