macOS 中的事件通知
Event notifications in macOS
macOS 应用程序是否可以侦听来自另一个应用程序的特定事件?
我想检测 Time Machine 备份何时启动,以便为 sparsebundle 所在的 NAS 文件夹创建时间点快照。
Time Machine 引擎发送分布式通知。
添加一个观察者
Objective-C
[[NSDistributedNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleNotifications:)
name:nil
object:nil];
Swift
DistributedNotificationCenter.default().addObserver(self, selector: #selector(handleNotifications), name: nil, object: nil)
并实现相应的选择器
Objective-C
- (void)handleNotifications:(NSNotification *)notification {
NSLog(@"%@", notification);
}
Swift
@objc func handleNotifications(_ notification : Notification) {
print(notification)
}
您必须过滤与 Time Machine 相关的通知。您还可以通过 name
参数
观察特定通知
macOS 应用程序是否可以侦听来自另一个应用程序的特定事件?
我想检测 Time Machine 备份何时启动,以便为 sparsebundle 所在的 NAS 文件夹创建时间点快照。
Time Machine 引擎发送分布式通知。
添加一个观察者
Objective-C
[[NSDistributedNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleNotifications:)
name:nil
object:nil];
Swift
DistributedNotificationCenter.default().addObserver(self, selector: #selector(handleNotifications), name: nil, object: nil)
并实现相应的选择器
Objective-C
- (void)handleNotifications:(NSNotification *)notification {
NSLog(@"%@", notification);
}
Swift
@objc func handleNotifications(_ notification : Notification) {
print(notification)
}
您必须过滤与 Time Machine 相关的通知。您还可以通过 name
参数