如何使用 CFNotificationCenterGetDarwinNotifyCenter() 发送 userInfo 字典

How can I send a userInfo dict using CFNotificationCenterGetDarwinNotifyCenter()

我需要使用 CFNotificationCenterGetDarwinNotifyCenter() 发送对象,但是,我注意到每当我使用

发送通知时
const void *tkeys[1] = { @"testKey" };
const void *tvalues[1] = { @"testValue" };
CFDictionaryRef userInfo = CFDictionaryCreate(NULL, tkeys, tvalues, 1, NULL, NULL);
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(),
                                       CFSTR("foo"),
                                       object,
                                       userInfo,
                                       true);

然后我发现在观察者回调中,userInfo dict 是 NULL。我在这里缺少什么吗?有没有其他方法可以发送 userInfo 对象?我正在查看 http://nshipster.com/inter-process-communication/ 和分布式通知部分,但是每当我使用 :

CFNotificationCenterRef distributedCenter =
CFNotificationCenterGetDistributedCenter();

然后我每次尝试都会收到错误 Invalid Use of Function 错误。

如果你阅读 the documentation for CFNotificationCenterGetDarwinNotifyCenter,你会发现这个警告:

Several function parameters are ignored by Darwin notification centers. To ensure future compatibility, you should pass NULL or 0 for all ignored arguments.

userInfo 是这些参数之一。 CFNotificationCenter.h 更详细:

// For this center, there are limitations in the API. There are no notification "objects",
// "userInfo" cannot be passed in the notification, ...
// - CFNotificationCenterPostNotification(): the 'object', 'userInfo', and 'deliverImmediately' arguments are ignored.

为什么?挖得更深一点。该文档还提到这种类型的通知中心是建立在 notify.h. If you look that up, you'll find that there is only one way to post a notification, by calling notify_post() 中声明的核心 OS 通知机制之上的,它只接受一个字符串参数。无法向通知添加其他数据。这是一个非常古老和基本的 IPC 机制,对 CoreFoundation 数据结构一无所知。

至于 CFNotificationCenterGetDistributedCenter,在 iOS 上不可用。