有没有办法防止Mac使用Objective-C以编程方式进入睡眠状态? Apple开发站点上的I/O工具包基础知识部分告诉我,驱动程序会收到有关空闲/系统睡眠的通知,但我找不到防止系统睡眠的方法.甚至有可能吗?
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 notified of an idle / system sleep, but I can't find a way of preventing the system from sleeping. Is it even possible?
我遇到了一些其他使用Caffeine,jiggler,sleepless甚至AppleScript的解决方案,但是我想在Objective-C中做到这一点.谢谢.
I've come across some other solutions using Caffeine, jiggler, sleepless and even AppleScript, but I want to do this in Objective-C. Thanks.
推荐答案这是Apple的官方文档(包括代码片段): 技术问答QA1340-如何防止睡眠?
Here is the official Apple documentation (including code snippet): Technical Q&A QA1340 - How to I prevent sleep?
报价::在Mac OS X 10.6 Snow Leopard中使用I/O Kit防止睡眠:
Quote: Preventing sleep using I/O Kit in Mac OS X 10.6 Snow Leopard:
#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. }对于较旧的OSX版本,请检查以下内容: 技术问答QA1160-如何在我的应用程序处于运行状态时防止系统休眠正在运行?
For older OSX version, check the following: Technical Q&A QA1160 - How can I prevent system sleep while my application is running?
引用: UpdateSystemActivity的用法示例(< 10.6的规范方法)
Quote: Example usage of UpdateSystemActivity (the canonical way for < 10.6)
#include <CoreServices/CoreServices.h> 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); }更多推荐
如何以编程方式防止Mac进入睡眠状态?
发布评论