If you want to avoid the Objective-C API, there's a C-based API underlying it. You can call IOPMAssertionCreateWithDescription()
with an assertion type of kIOPMAssertionTypePreventUserIdleSystemSleep
. That gives you an assertion ID reference. You cancel the assertion using IOPMAssertionRelease()
to match the create call and any IOPMAssertionRetain()
calls you may have made.
CFURLRef bundleURL = CFBundleCopyBundleURL(CFBundleGetMainBundle());
CFStringRef bundlePath = CFURLCopyFileSystemPath(bundleURL, kCFURLPOSIXPathStyle);
CFRelease(bundleURL);
IOPMAssertionID assertion;
IOReturn result = IOPMAssertionCreateWithDescription(kIOPMAssertionTypePreventUserIdleSystemSleep,
CFSTR("A name which makes sense for your app"),
CFSTR("Some details"), CFSTR("A human-readable reason"),
bundlePath, 0, NULL, &assertion);
CFRelease(bundlePath);
if (result != kIOReturnSuccess)
/* handle error */;
// ... do computation ...
IOPMAssertionRelease(assertion);