如何以编程方式防止 Mac 进入睡眠状态?

编程入门 行业动态 更新时间:2024-10-22 14:00:31
本文介绍了如何以编程方式防止 Mac 进入睡眠状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有没有办法防止 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 进入睡眠状态?

本文发布于:2023-11-27 12:17:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638040.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:睡眠   状态   方式   Mac

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!