使用Swift为OSX禁用睡眠/屏幕保护程序

时间:2022-11-02 16:52:51

I'm looking for a way to disable sleep mode and screensaver through my application using Swift. I know this question has been asked before, but none of the answers are current (at least for Swift; I don't know about Objective-C).

我正在寻找一种方法来使用Swift通过我的应用程序禁用睡眠模式和屏幕保护程序。我知道之前已经问过这个问题,但没有一个答案是最新的(至少对于Swift;我不知道Objective-C)。

I originally thought to use NSWorkspace.sharedWorkspace().extendPowerOffBy(requested: Int), but according to Apple's documentation, it is currently unimplemented.

我原本以为使用NSWorkspace.sharedWorkspace()。extendPowerOffBy(requested:Int),但根据Apple的文档,它目前尚未实现。

Any suggestions?

1 个解决方案

#1


2  

I recently came across this answer. It links to Q&A1340 at Apple, and translates listing 2 into Swift.

我最近遇到了这个答案。它链接到Apple的Q&A1340,并将清单2翻译成Swift。

I refactored it into some different code, that shows how you can use them throughout the runloop, for instance. I did check the code, and it works.

我将它重构为一些不同的代码,它们展示了如何在整个runloop中使用它们。我确实检查了代码,但它确实有效。

var assertionID: IOPMAssertionID = 0
var success: IOReturn?

func disableScreenSleep(reason: String = "Unknown reason") -> Bool? {
    guard success != nil else { return nil }
    success = IOPMAssertionCreateWithName( kIOPMAssertionTypeNoDisplaySleep as CFString,
                                           IOPMAssertionLevel(kIOPMAssertionLevelOn),
                                           reason as CFString,
                                           &assertionID )
    return success == kIOReturnSuccess
}

func  enableScreenSleep() -> Bool {
    if success != nil {
        success = IOPMAssertionRelease(noSleepAssertionID)
        success = nil
        return true
    }
    return false
}

The Q&A1340 answer also points out that using NSWorkspace.shared should only be used to support OS X < 10.6.

Q&A1340答案还指出,使用NSWorkspace.shared只应用于支持OS X <10.6。

#1


2  

I recently came across this answer. It links to Q&A1340 at Apple, and translates listing 2 into Swift.

我最近遇到了这个答案。它链接到Apple的Q&A1340,并将清单2翻译成Swift。

I refactored it into some different code, that shows how you can use them throughout the runloop, for instance. I did check the code, and it works.

我将它重构为一些不同的代码,它们展示了如何在整个runloop中使用它们。我确实检查了代码,但它确实有效。

var assertionID: IOPMAssertionID = 0
var success: IOReturn?

func disableScreenSleep(reason: String = "Unknown reason") -> Bool? {
    guard success != nil else { return nil }
    success = IOPMAssertionCreateWithName( kIOPMAssertionTypeNoDisplaySleep as CFString,
                                           IOPMAssertionLevel(kIOPMAssertionLevelOn),
                                           reason as CFString,
                                           &assertionID )
    return success == kIOReturnSuccess
}

func  enableScreenSleep() -> Bool {
    if success != nil {
        success = IOPMAssertionRelease(noSleepAssertionID)
        success = nil
        return true
    }
    return false
}

The Q&A1340 answer also points out that using NSWorkspace.shared should only be used to support OS X < 10.6.

Q&A1340答案还指出,使用NSWorkspace.shared只应用于支持OS X <10.6。