mh *_*qia 5 macos cocoa macos-carbon postmessage
在Windows中,我可以将自定义消息发布到另一个进程,并通知它执行以下操作:
PostMessage(WindowOfAnyProcess, WM_CUSTOM_MESSAGE, param1, param2)
Run Code Online (Sandbox Code Playgroud)
在Mac OS上有什么替代方案?碳活动对我有帮助吗?怎么样?谢谢。
假设这两个过程都属于您,则可以使用NSDistributedNotificationCenter将通知和数据发送到每个过程。
为此,请执行以下操作:
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"HelloFromProcessOne" object:nil]
Run Code Online (Sandbox Code Playgroud)
如果要包括数据,可以使用:
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"HelloFromProcessOne" object:nil userInfo:[NSDictionary dictionaryWithObject:@"some info here" forKey:@"data"]]
Run Code Online (Sandbox Code Playgroud)
应该添加一个注释:
沙盒应用程序仅在不包含字典的情况下才能发送通知。如果发送的应用程序在“应用程序沙箱”中,则notificationInfo必须为nil。这意味着,如果您打算定位Mac AppStore,则将无法在通知中提供信息。
要使应用程序接收通知,请执行以下操作:
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(someNotificationUpdate:) name:@"HelloFromProcessOne" object:nil]
Run Code Online (Sandbox Code Playgroud)
someNotificationUpdate: 将被声明为:
- (void)someNotificationUpdate:(NSNotification *)note;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1928 次 |
| 最近记录: |