【发布时间】:2012-11-03 18:02:26
【问题描述】:
iOS 上的私有 AppSupport 框架有一个名为 CPDistributedNotificationCenter 的类,它似乎支持 OS X 上 NSDistributedNotificationCenter 提供的功能的子集。
我正在尝试使用此类从后台守护程序发布通知,以便其他进程中的多个客户端可以接收这些通知并对其采取行动。我意识到还有其他选择,包括CPDistributedMessagingCenter 或CFMessagePort、低级马赫端口甚至达尔文的notify_post。但是,如果守护进程不了解客户端,我会更喜欢它,并且我希望能够与通知一起传递数据,而 notify_post 不允许这样做。
目前,这是我在守护进程中所做的:
CPDistributedNotificationCenter* center;
center = [CPDistributedNotificationCenter centerNamed:@"com.yfrancis.notiftest"];
[center runServer];
[center postNotificationName:@"hello"];
在客户端:
CPDistributedNotificationCenter* center;
center = [CPDistributedNotificationCenter centerNamed:@"com.yfrancis.notiftest"];
[center startDeliveringNotificationsToMainThread];
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc addObserver:[Listener new]
selector:@selector(gotNotification:)
name:@"hello"
object:nil];
其中Listener 是一个实现单个方法gotNotification: 的简单类
不幸的是,客户端从未收到“你好”通知。如果我用nil 替换addObserver 调用中的name 参数,我可以看到发送到客户端通知中心的每个通知,但“你好”不是其中之一。
我通过查看 SpringBoard 和 CPDistributedNotificationCenter 的反汇编获得了我的代码的灵感。通知似乎是通过CPDistributedNotificationCenter 的deliverNotification:userInfo: 传递的,它充当NSNotificationCenter 的postNotificationName:object:userInfo: 的垫片。
我在这里错过了什么?
【问题讨论】:
-
XPC 怎么样,每个进程都可以与守护进程保持双向连接?还是XPC,加上notify_post?一个缺点是 XPC 是 iOS5+。我认为 XPC 是使用守护进程作为后端的库的新标准。
-
我不希望守护进程对客户端有任何了解。 XPC 打败了这一点,我只想触发一次性通知,如果有任何感兴趣的客户,他们可以对其采取行动。
-
Activator 使用注册机制,
CPDistributedMessagingCenter需要远程中心名称才能向其发送消息,因此需要了解客户端
标签: objective-c ios cocoa ipc