在OS X上以编程方式检查请勿打扰的状态 [英] Programmatically check state of do not disturb on OS X

查看:72
本文介绍了在OS X上以编程方式检查请勿打扰的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Objective-C,如何以编程方式检查OS X上系统请勿打扰"设置的状态?我不需要使用Mac或App Store,因为我不需要提交到Mac App Store.

Using Objective-C, how can I programmatically check the state of the system "Do Not Disturb" setting on OS X? I'm fine with using hacks or private APIs since I don't need to submit to the Mac App Store.

推荐答案

您可以(并且应该)简单地使用UserDefaults:

You can (and should) simply use UserDefaults:

let theDefaults = UserDefaults(suiteName: "com.apple.notificationcenterui")
print(theDefaults?.bool(forKey: "doNotDisturb"))

对于时间控制的切换,您应该检查一天中的分钟是否在dndStartdndEnd之间:

For time-controlled switching you should check if the minute of the day lies between dndStart and dndEnd:

let theDefaults = UserDefaults(suiteName: "com.apple.notificationcenterui")
let theDate = Date()
let theCalendar = Calendar.current

let theHour = calendar.component(.hour, from: theDate)
let theMinute = calendar.component(.minute, from: theDate)
let theMinuteOfDay = Double(theHour * 60 + theMinute)

if theMinuteOfDay >= theDefaults.double(forKey: "dndStart") && 
    theMinuteOfDay <= theDefaults.double(forKey: "dndEnd") {
    // ...
}

这篇关于在OS X上以编程方式检查请勿打扰的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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