DaneOzzo
DaneOzzo

Reputation: 53

Prevent Sleep on Mac OS

my application doesn't seem to be able to prevent system sleep on Mac OS Mountain Lion.

Here is what it should do:

A Cocoa application gets launched at a certain time using the launchd process. Preventing system sleep is done at launch of the application using IOPMAssertionCreateWithName. The application simply has a (hidden) WebView, calls several URLs, parses the content, sends results to a webserver, quits.

Here is my code to prevent sleep:

- (void)preventSleep
{
    CFStringRef reasonForActivity= CFSTR("Content parsing");

    IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep,
                                                   kIOPMAssertionLevelOn, reasonForActivity, &self.assertionID);
    if (success == kIOReturnSuccess)
    {
        Log([NSColor orangeColor], @"Prevent sleep\n");
    }
}

This is what is going on:

The launchd process works fine. The sleeping system wakes up, the application starts, prints "Prevent sleep" in my log, parses some URLs, but suddenly after a few seconds the system wents to sleep.

What is happening?

Is IOPMAssertionCreateWithName maybe not working with applications launched by launchd on a system with no user activity?

What can i do to prevent the system from sleeping while my application is processing?

Upvotes: 2

Views: 1906

Answers (1)

MrAsterisco
MrAsterisco

Reputation: 888

OS X 10.7 will return to sleep, if no user activity is detected. You can simulate user activity using the following function:

UpdateSystemActivity(OverallAct);

Please, note that this will turn the screen on.

Upvotes: 0

Related Questions