Using Swift to Disable Sleep/Screen Saver for Osx

How to disable the screen saver/display sleep on Mac

i've just solved it and forgot to post it as an answer. Calling UpdateSystemActivity(OverallAct) every 30 seconds works.

Stopping Mac from sleeping while app is running

As I already mentioned in comments you need to remove the guard otherwise it will simply return nil and will never disable the screen sleep. You can also simplify your method, return void and change your success property to a Bool property like disabled to monitor the screen sleep state:

var assertionID: IOPMAssertionID = 0
var sleepDisabled = false
func disableScreenSleep(reason: String = "Disabling Screen Sleep") {
if !sleepDisabled {
sleepDisabled = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep as CFString, IOPMAssertionLevel(kIOPMAssertionLevelOn), reason as CFString, &assertionID) == kIOReturnSuccess
}

}
func enableScreenSleep() {
if sleepDisabled {
IOPMAssertionRelease(assertionID)
sleepDisabled = false
}

}

To disable the screen simply just call the method inside viewDidLoad of your first view controller:

override func viewDidLoad() {
super.viewDidLoad()
disableScreenSleep()
print("sleep Disabled:", sleepDisabled)
}

Programmatically change screen saver settings

As I mentioned in Cocoa-Dev list, I was able to do it this way.

Reading:

var moduleDict = CFPreferencesCopyAppValue("moduleDict", "com.apple.screensaver") as NSDictionary
var saverName = moduleDict["moduleName"] as String!

Writing:

var moduleDict = CFPreferencesCopyAppValue("moduleDict", "com.apple.screensaver") as NSDictionary
var mutable = moduleDict.mutableCopy() as NSMutableDictionary
mutable["moduleName"] = "MyScreenSaver"
mutable["path"] = mySaverPath
CFPreferencesSetValue("moduleDict", mutable as CFPropertyList, "com.apple.screensaver", kCFPreferencesCurrentUser, kCFPreferencesCurrentHost)
CFPreferencesAppSynchronize("com.apple.screensaver")

What is the correct way to prevent sleep on OS X?

IOCancelPowerChange continues to work but only for idle triggered sleep; it will not work for sleep triggered by the Finder's Sleep menu item, programmatically requested, or from a push of the power button.

Apple's Q&A1340 answers the question "Q: How can my application get notified when the computer is going to sleep or waking from sleep? How do I prevent sleep?"

Listing 2 of Q&A1340:

#import <IOKit/pwr_mgt/IOPMLib.h>

// kIOPMAssertionTypeNoDisplaySleep prevents display sleep,
// kIOPMAssertionTypeNoIdleSleep prevents idle sleep

//reasonForActivity is a descriptive string used by the system whenever it needs
// to tell the user why the system is not sleeping. For example,
// "Mail Compacting Mailboxes" would be a useful string.

// NOTE: IOPMAssertionCreateWithName limits the string to 128 characters.
CFStringRef* reasonForActivity= CFSTR("Describe Activity Type");

IOPMAssertionID assertionID;
IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep,
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.
}

Note that you can only stop idle time sleep, not sleep triggered by the user.

For applications supporting Mac OS X 10.6 and later, use the new IOPMAssertion family of functions. These functions allow other applications and utilities to see your application's desire not to sleep; this is critical to working seamlessly with third party power management software.

How to prevent screen lock on my application with swift on iOS

Use this:

Objective-C:

[[UIApplication sharedApplication] setIdleTimerDisabled: YES];

Swift (legacy):

UIApplication.sharedApplication().idleTimerDisabled = true

Swift 3 and above:

UIApplication.shared.isIdleTimerDisabled = true

Make sure to import UIKit.

Here is the link to the documentation from developer.apple.com.

Prevent Shutdown/Sleep on Macs

In Carbon's Application Manager, there's the SetSystemUIMode API which lets you control (disable) things like force-quit, the power-key window etc. It's intended for kiosk style applications and described in this tech note.

Disabling sleep, screen-saver, etc. is done by periodically calling UpdateSystemActivity. See this tech Q&A.



Related Topics



Leave a reply



Submit