Latest 0.3.0
Homepage https://github.com/jesse-c/PreventSleep
License MIT
Platforms osx 10.10, requires ARC
Authors

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.

Latest podspec

{
    "name": "PreventSleep",
    "version": "0.3.0",
    "summary": "PreventSleep is a simple library to prevent a Mac from allowing a display to sleep, or from prevent idle sleep.",
    "homepage": "https://github.com/jesse-c/PreventSleep",
    "license": {
        "type": "MIT",
        "file": "LICENSE.md"
    },
    "authors": {
        "Jesse Claven": "[email protected]"
    },
    "platforms": {
        "osx": "10.10"
    },
    "source": {
        "git": "https://github.com/jesse-c/PreventSleep.git",
        "tag": "0.3.0"
    },
    "source_files": "Sources/*.swift",
    "requires_arc": true,
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This