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 {
source share