在Mac上的Swift 4上没有收到ScreenLock通知 [英] Not getting screenLock notification on Swift 4 on mac

查看:104
本文介绍了在Mac上的Swift 4上没有收到ScreenLock通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我没有收到ScreenIsLocked和ScreenIsUnlocked通知.我定义了屏幕保护程序启动后0秒钟屏幕被锁定,但没有日志:

For some reason I'm not getting the ScreenIsLocked and ScreenIsUnlocked notifications. I defined that the screen get locked 0 seconds after the screen saver starts and yet no log:

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的帖子:

This is from a post by 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)

这篇关于在Mac上的Swift 4上没有收到ScreenLock通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆