2

当其他应用程序打开或退出时,或者当不同的窗口(监听应用程序或其他)被带到最前沿时,是否有任何方法可以通知可可应用程序?

4

1 回答 1

3

您可以将观察者添加到共享工作区通知中心:

[[[NSWorkspace sharedWorkspace] notificationCenter] addObserverForName:NSWorkspaceDidActivateApplicationNotification object:nil queue:[NSOperationQueue currentQueue] usingBlock:^(NSNotification *note) {
    NSRunningApplication *frontmostApplication = [[note userInfo] objectForKey:NSWorkspaceApplicationKey];
}];

// from NSWorkspace.h
APPKIT_EXTERN NSString * NSWorkspaceWillLaunchApplicationNotification;  //  see above
APPKIT_EXTERN NSString * NSWorkspaceDidLaunchApplicationNotification;   //  see above
APPKIT_EXTERN NSString * NSWorkspaceDidTerminateApplicationNotification;    //  see above
APPKIT_EXTERN NSString * const NSWorkspaceDidHideApplicationNotification NS_AVAILABLE_MAC(10_6);
APPKIT_EXTERN NSString * const NSWorkspaceDidUnhideApplicationNotification NS_AVAILABLE_MAC(10_6);
APPKIT_EXTERN NSString * const NSWorkspaceDidActivateApplicationNotification NS_AVAILABLE_MAC(10_6);
APPKIT_EXTERN NSString * const NSWorkspaceDidDeactivateApplicationNotification NS_AVAILABLE_MAC(10_6);
于 2014-12-09T23:14:26.887 回答