【发布时间】:2021-05-15 09:23:10
【问题描述】:
我试图在另一个应用程序使用 Accessibility API 和 AXObserver 创建窗口时收到通知。这是我的代码:
let pid = NSWorkspace.shared.frontmostApplication!.processIdentifier
var observer: AXObserver?
if AXObserverCreate(pid, { (observer, element, notification, userData) in
// Does this get executed when a notification comes in?
print(notification)
}, &observer) == .success {
// This does get printed!
print("Successfully created Observer!")
}
if AXObserverAddNotification(observer!, element, notification, nil) == .success {
// This also gets printed!
print("Successfully added Notification!")
}
CFRunLoopAddSource(RunLoop.current.getCFRunLoop(), AXObserverGetRunLoopSource(observer!), CFRunLoopMode.defaultMode)
我错过了什么吗?代码编译并运行,但我的回调没有被执行。 有通知进来时回调会执行吗?
【问题讨论】:
-
observer超出范围时是否被释放?