原文地址
http://www.cnblogs.com/chen1987lei/p/4240001.html
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];
复制代码