简体   繁体   English

如何使用CFNotificationCenterGetDarwinNotifyCenter()发送userInfo dict

[英]How can I send a userInfo dict using CFNotificationCenterGetDarwinNotifyCenter()

I need to send an object using CFNotificationCenterGetDarwinNotifyCenter() however, I'm noticing that whenever I send a notification using 我需要使用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);

then I find that in the observer callback, the userInfo dict is NULL. 然后我发现在观察者回调中,userInfo dict为NULL。 Is there something I am missing here? 这里有什么我想念的吗? Is there some other way by which I can possibly send the userInfo object? 是否有其他方式可以发送userInfo对象? I was looking at http://nshipster.com/inter-process-communication/ and the Distributed Notifications part, however whenever I use : 我正在查看http://nshipster.com/inter-process-communication/和分布式通知部分,但每当我使用时:

CFNotificationCenterRef distributedCenter =
CFNotificationCenterGetDistributedCenter();

then I get an error Invalid Use of Function error whenever I try it. 然后Invalid Use of Function每当我尝试它时,我都会收到错误Invalid Use of Function错误。

If you read the documentation for CFNotificationCenterGetDarwinNotifyCenter , you'll find this caveat: 如果您阅读CFNotificationCenterGetDarwinNotifyCenter的文档 ,您会发现以下警告:

Several function parameters are ignored by Darwin notification centers. Darwin通知中心忽略了几个功能参数。 To ensure future compatibility, you should pass NULL or 0 for all ignored arguments. 为确保将来的兼容性,您应该为所有被忽略的参数传递NULL或0。

userInfo is one of those parameters. userInfo是其中一个参数。 CFNotificationCenter.h goes into more detail: 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.

Why? 为什么? Dig a little deeper. 深入挖掘一下。 The documentation also mentions that this type of notification center is built on the Core OS notify mechanism declared in notify.h . 该文档还提到这种类型的通知中心是基于notify.h声明的Core OS通知机制notify.h If you look that up, you'll find that there is only one way to post a notification, by calling notify_post() , which takes only a single string argument. 如果你仔细观察,你会发现只有一种方法可以发布通知,方法是调用notify_post() ,它只需要一个字符串参数。 There is no way to add additional data to the notification. 无法向通知添加其他数据。 This is a very old and rudimentary IPC mechanism that doesn't know anything about CoreFoundation data structures. 这是一个非常古老而基本的IPC机制,它对CoreFoundation数据结构一无所知。

As for CFNotificationCenterGetDistributedCenter , that isn't available on iOS. 至于CFNotificationCenterGetDistributedCenter ,这在iOS上不可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在UILocalNotification中更改UserInfo? - How i can change UserInfo in UILocalNotification? 如何在 Swift 中使用 NSNotification -> userInfo 发送对象数组 - how to send array of objects using NSNotification -> userInfo in Swift 使用NSTimer时如何更改userInfo中的数据? - How to change data in the userInfo when using NSTimer? 如何使用MFMailComposeViewController发送电子邮件 - How can I send email using MFMailComposeViewController 如何在应用程序中解析userInfo字典(应用程序:UIApplication,didReceiveRemoteNotification userInfo:[NSObject:AnyObject]) - How do I parse the userInfo dictionary in application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) 我无法获取ShortcutItem。 userInfo的值 - I can't get shortcutItem. The value of the userInfo 如何使用 WatchConnectivity 将数据发送回 iPhone - How can I send a data back to iPhone using WatchConnectivity 如何使用PushMagic令牌发送JavaPNSpayloads - How can i send JavaPNSpayloads using PushMagic token 如何使用Google SMTP发送邮件(错误) - How can I send mail using Google SMTP (an error) 我如何使用 websocket 将音频发送到 Microsoft Translator - How can i send Audio using websocket to Microsoft Translator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM