Disable hibernation in OS X with fast

I made an OS X application in Xcode, and I want my Mac to stay awake when I open it. I know that in iOS Swift you use:

UIApplication.sharedApplication().idleTimerDisabled = true 

But how do you do this with OS X Swift?

0
source share
2 answers

The current method is shown in Technical QA 1340 . This is supposedly about nighttime sleep and wake reports, but see list 2, entitled "Preventing sleep with the I / O Kit on Mac OS X 10.6 Snow Leopard." Basically, you use IOPMAssertionCreateWithName to enter a state in which sleep is disabled, then call IOPMAssertionRelease when you IOPMAssertionRelease done.

I don’t have a code sample since I personally didn’t use it, but it would be quite simple to transfer the code to a technology note in Swift.

Update: This API was introduced in 10.6, but it still works fine in the latest OS, and as far as I know, is still the preferred way to do this. Also works in Swift.

 import IOKit import IOKit.pwr_mgt let reasonForActivity = "Reason for activity" as CFString var assertionID: IOPMAssertionID = 0 var success = IOPMAssertionCreateWithName( kIOPMAssertionTypeNoDisplaySleep as CFString, IOPMAssertionLevel(kIOPMAssertionLevelOn), reasonForActivity, &assertionID ) if success == kIOReturnSuccess { // Add the work you need to do without the system sleeping here. success = IOPMAssertionRelease(assertionID); // The system will be able to sleep again. } 
+1
source

If you are trying to prevent inactivity from starting, IOCancelPowerCharge may work. But it will not work if something manually causes a dream

0
source

All Articles