【发布时间】:2011-11-07 04:41:32
【问题描述】:
我正在为 TextEdit 开发一个 SIMBL 插件,该插件添加了一个额外的菜单。菜单有许多项目在选择(以及其他内容)时发送消息到我自己的应用程序。为此,我一直在使用 NSDistributedNotificationCenter,但是我无法从 TextEdit 中的包发送通知。
我检查了我在一个单独的应用程序中使用的代码,它似乎可以工作,所以我不明白为什么我在 TextEdit 中的包无法发送消息。
发布通知代码:
NSDictionary *user = [NSDictionary dictionaryWithObjectsAndKeys:
@"TextEdit", @"applicationName",
@"289", @"applicationVersion",
@"1", @"menuItem", nil];
NSString *observedObject = @"com.drake.DDX";
NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
[center postNotificationName:@"DDXNotification" object:observedObject userInfo:user deliverImmediately:YES];
以及接收器中的代码:
NSString *observedObject = @"com.drake.DDX";
NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(receiveNotification:) name:@"DDXNotification" object:observedObject];
如果我将这段代码添加到我的另一个应用程序中,它似乎可以工作,所以它让我认为 TextEdit 或 SIMBL 中发生了一些我不知道的事情。
我检查了菜单项是否正确执行该方法,方法是在执行该方法后立即在 TextEdit 中显示一个 NSAlert。
感谢任何帮助。
(编辑)我发现当我发布通知时,如果 userInfo 字典为 nil,我的应用程序会收到通知,因此我发送的 NSDictionary 或接收器方法签名似乎存在问题。
【问题讨论】:
-
您已正确识别问题。在沙盒下,您无法通过通知传递 userInfo 有效负载。
标签: objective-c macos plugins osx-lion simbl