How to programmatically prevent a Mac from going to sleep?

后端 未结 3 1978
轮回少年
轮回少年 2020-11-27 17:24

Is there way to prevent a Mac from going to sleep programmatically using Objective-C? The I/O kit fundamentals section on Apple\'s dev site tells me that a driver gets notif

3条回答
  •  自闭症患者
    2020-11-27 18:07

    Here is the official Apple documentation (including code snippet):
    Technical Q&A QA1340 - How to I prevent sleep?

    Quote: Preventing sleep using I/O Kit in Mac OS X 10.6 Snow Leopard:

    #import 
    
    // 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. 
    }
    

    For older OSX version, check the following:
    Technical Q&A QA1160 - How can I prevent system sleep while my application is running?

    Quote: Example usage of UpdateSystemActivity (the canonical way for < 10.6)

    #include 
    
    void
    MyTimerCallback(CFRunLoopTimerRef timer, void *info)
    {
        UpdateSystemActivity(OverallAct);
    }
    
    
    int
    main (int argc, const char * argv[])
    {
        CFRunLoopTimerRef timer;
        CFRunLoopTimerContext context = { 0, NULL, NULL, NULL, NULL };
    
        timer = CFRunLoopTimerCreate(NULL, CFAbsoluteTimeGetCurrent(), 30, 0, 0, MyTimerCallback, &context);
        if (timer != NULL) {
            CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer, kCFRunLoopCommonModes);
        }
    
        /* Start the run loop to receive timer callbacks. You don't need to
        call this if you already have a Carbon or Cocoa EventLoop running. */
        CFRunLoopRun();
    
        CFRunLoopTimerInvalidate(timer);
        CFRelease(timer);
    
        return (0);
    }
    

提交回复
热议问题