No description, website, or topics provided.
Swift Ruby Objective-C
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
PreventSleep.xcodeproj
PreventSleep.xcworkspace
Sources
Tests
CHANGELOG.md
CONTRIBUTING.md
LICENSE.md
PreventSleep.podspec
README.md

README.md

PreventSleep

Carthage compatible CocoaPods compatible License

PreventSleep is a simple library to prevent a Mac from allowing a display to sleep, or from prevent idle sleep.

It differs from another common approach on Mac which is to handle a caffeinate process.

Example

import PreventSleep
import IOKit.pwr_mgt

let preventSleep = PreventSleep(
    sleepAssertionMsg: "Prevent idle sleep when playing audio.",
    sleepAssertionType: kIOPMAssertionTypeNoIdleSleep
)!

preventSleep.preventSleep()

sleep(10)

preventSlep.allowSleep()

To check that the power assertion was correctly created/removed, run pmset -g assertions and look for your assertion.

Installation

Carthage

github "jesse-c/PreventSleep" ~> VERSION_NUMBER

Usage

Initialisation

PreventSleep(sleepAssertionMsg: String, sleepAssertionType)

sleepAssertionMsg: String is the message that will be attached to the power assertion. sleepAssertionType: String is either kIOPMAssertionTypeNoIdleSleep or kIOPMAssertionTypeNoDisplaySleep which are available after importing IOKit.pwr_mgt.

Prevent sleeping

preventSleep() -> Bool

Indefinitely prevents the type of sleep specified. Returns true if a power assertion was create successfully, false otherwise.

Allow sleeping

allowSleep() -> Bool

Allow the Mac to go to sleep following the user's energy prefernces. Returns true if a power assertion was removed successfully, false otherwise.

Check current sleeping status

canSleep() -> Bool

Returns true if there is currently no power assertion preventing sleeping, false otherwise.

Contributing

See Contributing.