各种 kIOPMAssertionType 之间的差异

Ale*_*lex 6 objective-c iokit power-management

kIOPMAssertionTypeNoIdleSleep,kIOPMAssertionTypePreventSystemSleep和有什么区别kIOPMAssertionTypePreventUserIdleSystemSleep

我正在尝试创建一个IOPMAssertion可以防止 mac 自动进入睡眠状态的程序,但我真的不知道应该使用哪一个。我对他们的描述感到困惑,无法理解它们(请参阅文档

如果您好奇,这就是我在代码中执行此操作的方式:

IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, CFSTR("My app is running"), &preventSleepAssertionID);

if (success != kIOReturnSuccess) {
    NSLog(@"Could not create sleep prevention assertion");
}
Run Code Online (Sandbox Code Playgroud)

pmd*_*mdj 2

苹果已经发布了有关此主题的问答说明,我相信它可以回答您的问题。相关示例代码中的关键注释:

\n\n
// kIOPMAssertionTypeNoDisplaySleep prevents display sleep,\n// kIOPMAssertionTypeNoIdleSleep prevents idle sleep\n
Run Code Online (Sandbox Code Playgroud)\n\n

前者可以防止屏幕变暗或完全关闭。如果您的应用程序将以用户不使用键盘和鼠标的方式使用,例如视频播放器或视频聊天,请使用此选项。

\n\n

后者会阻止系统本身进入睡眠状态,但允许屏幕变暗并最终完全关闭。对于长时间运行的计算和仅需要音频的应用程序很有用。

\n\n

实际的代码反映了您所得到的:

\n\n
//reasonForActivity is a descriptive string used by the system whenever it needs \n//  to tell the user why the system is not sleeping. For example, \n//  "Mail Compacting Mailboxes" would be a useful string.\n\n//  NOTE: IOPMAssertionCreateWithName limits the string to 128 characters. \nCFStringRef* reasonForActivity= CFSTR("Describe Activity Type");\n\nIOPMAssertionID assertionID;\nIOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep, \n                                    kIOPMAssertionLevelOn, reasonForActivity, &assertionID); \nif (success == kIOReturnSuccess)\n{\n\n    //Add the work you need to do without \n    //  the system sleeping here.\n\n    success = IOPMAssertionRelease(assertionID);\n    //The system will be able to sleep again. \n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

用户显式触发睡眠(例如,关闭盖子或在 \xef\xa3\xbf 菜单中选择它)或系统使用电池供电时,仍然可以覆盖电源断言。

\n\n

您在使代码正常工作时遇到困难吗?

\n