How To Prevent The Screen Saver In El Capitan

Hi,


I've been using


IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep, kIOPMAssertionLevelOn, reasonForActivity, &assertionID);


as detailed in this Tech Q & A to prevent the screen saver from activating in certain phases of my app. Unfortunately, since 10.11 this seems to have stopped working. There are no errors, no extra log lines generated, the app works absolutely fine and the only difference in it's operation - in El Capitan - is that the screen saver continues to show after the prerequisite amount of time.


Does anyone know if there's been a change to the process of preventing this? I can't seem to find anything in docs...


Todd.

Accepted Reply

Interesting. I can still get power assertions to work on El Capitan, using this code:

IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep, kIOPMAssertionLevelOn,(CFStringRef)@"Preventing Sleep", &_assertionID1)


I don't know why your code seems to have stopped working. Have you verified that the code works under Yosemite? Make sure you use an identical build of the app, then test on both platforms. It seems likely to me that something you did is keeping it from working. It could be an unintentional side effect that happens only on El Capitan, but I still think it might have something to do with your code.


You might also want to try using the new power assertion API introduced back in OS X Mavericks (10.9, released in 2013), which looks like this:

id token = [NSProcessInfo.processInfo beginActivityWithOptions:NSActivityIdleDisplaySleepDisabled reason:@"Preventing Sleep"];
...
[NSProcessInfo.processInfo endActivity:token];

This newer API might not have the same side effects—it's worth a try.

Replies

Interesting. I can still get power assertions to work on El Capitan, using this code:

IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep, kIOPMAssertionLevelOn,(CFStringRef)@"Preventing Sleep", &_assertionID1)


I don't know why your code seems to have stopped working. Have you verified that the code works under Yosemite? Make sure you use an identical build of the app, then test on both platforms. It seems likely to me that something you did is keeping it from working. It could be an unintentional side effect that happens only on El Capitan, but I still think it might have something to do with your code.


You might also want to try using the new power assertion API introduced back in OS X Mavericks (10.9, released in 2013), which looks like this:

id token = [NSProcessInfo.processInfo beginActivityWithOptions:NSActivityIdleDisplaySleepDisabled reason:@"Preventing Sleep"];
...
[NSProcessInfo.processInfo endActivity:token];

This newer API might not have the same side effects—it's worth a try.

Good answer, thanks for that.


I can't find any bug in the code, but a restart of the machine seemed to fix it! However, you're right to point out the newer API so I'm using that.

I've found the answer: I was accessing the test machine with Screen Sharing. Everytime I quit Screen sharing on my Mac, it caused the Screen Saver to start on the controlled machine.


I believe that may be a bug.

Remember that the power assertions prevent sleep and display sleep from kicking in automatically—they do not prevent sleep from being intentionally triggered. It could still be a bug, though.

I'm on MacOS 12.1

i've tried:

ProcessInfo.processInfo.performActivity(options: [.idleDisplaySleepDisabled, .userInitiated], reason: "...")

IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep as CFString, UInt32(kIOPMAssertionLevelOn), "..." as CFString, &assertionID)

IOPMAssertionDeclareUserActivity("..." as CFString, kIOPMUserActiveLocal, &self.assertionID)

they all prevent display sleep fine however, neither of these prevent the screen saver ("Preferences>Desktop & Screen Saver") from starting

at a loss, have no idea other than to tell users to turn off the screen saver. if they have display sleep turned on they shouldn't be needing it ever anyways.