CFRunLoop 守护进程中的应用程序更改通知

App Changed Notifications in CFRunLoop Daemon

static void registerForDriverLoadedNotification()
{
    // Snipped code that works and is not related to issue
}

static void registerForApplicationChangedNotification()
{
    NSLog(@"registerForApplicationChangedNotification!");
    NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
    [center addObserverForName: nil object: nil queue: nil
            usingBlock: ^(NSNotification* notification) {
                          NSLog(@"NOTIFICATION %@ -> %@", notification.name,
                                                          notification.userInfo);
                        }];
}


int main (int argc, const char* argv[]) {
    registerForDriverLoadedNotification();
    registerForApplicationChangedNotification();

    CFRunLoopRun();

    return 0;
}

以上代码是一个守护进程,它等待插入USB设备,然后加载配置。我想扩展此功能以检测应用程序何时启动,以及是否存在特定于应用程序的配置加载它。

但是,我似乎没有收到除 NSUserDefaultsDidChangeNotification 以外的任何通知。

上面 registerForApplicationChangedNotification 中的代码最初同时监视 NSWorkspaceDidActivateApplicationNotificationNSWorkspaceDidDeactivateApplicationNotification,但我将其更改为包罗万象,以便我可以看到发布了哪些其他通知。

无论发生什么,似乎只收到 NSUserDefaultsDidChangeNotification 通知...这个相当简单的代码有什么问题?

愚蠢的错误!

NSNotificationCenter* center = [NSNotificationCenter defaultCenter];

应该是:

NSNotificationCenter* center = [[NSWorkspace sharedWorkspace] notificationCenter]