更新时间:2023-01-25 21:53:06
如文档所示,您可以使用键值观察来观察 runningApplications 属性:
As the document suggests, you use Key-Value Observing to observe the runningApplications
property of the shared workspace object:
static const void *kMyKVOContext = (void*)&kMyKVOContext;
[[NSWorkspace sharedWorkspace] addObserver:self
forKeyPath:@"runningApplications"
options:NSKeyValueObservingOptionNew // maybe | NSKeyValueObservingOptionInitial
context:kMyKVOContext];
然后,您将实现观察方法(使用Xcode的现成代码段):
Then, you would implement the observation method (using Xcode's ready-made snippet):
- (void) observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context
{
if (context != kMyKVOContext)
{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
return;
}
if ([keyPath isEqualToString:@"runningApplications"])
{
<#code to be executed when runningApplications has changed#>
}
}