gpt4 book ai didi

swift - 在 mac 上的 Swift 4 中没有收到屏幕锁定通知

转载 作者:行者123 更新时间:2023-11-28 14:50:23 26 4
gpt4 key购买 nike

出于某种原因,我没有收到 ScreenIsLocked 和 ScreenIsUnlocked 通知。我定义屏幕保护启动后 0 秒锁定屏幕,但没有日志:

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

@objc func screenLocked() {
NSLog("yes")
}

func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application

NotificationCenter.default.addObserver(
self,
selector: #selector(AppDelegate.screenLocked),
name: Notification.Name("com.apple.screenIsLocked"),
object: nil)

NotificationCenter.default.addObserver(
self,
selector: #selector(AppDelegate.screenLocked),
name: Notification.Name("com.apple.screenIsUnlocked"),
object: nil)

NotificationCenter.default.addObserver(
self,
selector: #selector(AppDelegate.screenLocked),
name: Notification.Name("com.apple.screensaver.didstart"),
object: nil)

NotificationCenter.default.addObserver(
self,
selector: #selector(AppDelegate.screenLocked),
name: Notification.Name("com.apple.screensaver.didstop"),
object: nil)


}

func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application

}


}

最佳答案

使用NSDistributedNotificationCenter

这是来自 Kane Cheshire 的帖子:

https://kanecheshire.com/blog/2014/10/13/351/

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

NSDistributedNotificationCenter *center = [NSDistributedNotificationCenter defaultCenter];
[center addObserver:self
selector:@selector(screenLocked)
name:@"com.apple.screenIsLocked"
object:nil];

[center addObserver:self
selector:@selector(screenUnlocked)
name:@"com.apple.screenIsUnlocked"
object:nil];
}

在 Swift 中:

DistributedNotificationCenter.default().addObserver(self,
selector: #selector(screenLocked),
name: "com.apple.screenIsLocked",
object: nil)

DistributedNotificationCenter.default().addObserver(self,
selector: #selector(screenLocked),
name: "com.apple.screenIsUnlocked",
object: nil)

关于swift - 在 mac 上的 Swift 4 中没有收到屏幕锁定通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49914330/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com