如何关闭 Mac 屏幕保护程序?

发布于 2024-09-04 22:41:55 字数 146 浏览 4 评论 0原文

我正在编写一个使用 Apple 的信息亭模式的应用程序。我想禁用屏幕保护程序,但“ScreenSaverDefaults”类将其自身报告为仅 32 位。我可以将构建更改为仅 32 位,但我也希望能够支持 64 位架构。

我还应该使用其他框架来禁用屏幕保护程序吗?

I'm writing an application that uses Apple's kiosk mode. I would like to disable the screen saver, but the "ScreenSaverDefaults" class reports itself as being 32-bit only. I can change the build to be 32-bit only, but I would like to be able to support 64-bit architectures as well.

Are there any other Frameworks that I should use to disable the screen saver?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

转角预定愛 2024-09-11 22:41:55

首先,您需要保存当前设置,以便可以将其恢复到关闭之前的状态:

NSTask *readTask = [[NSTask alloc] init];
[readTask setLaunchPath:@"/usr/bin/defaults"];

NSArray *arguments = [NSArray arrayWithObjects:@"-currentHost", @"read", @"com.apple.screensaver", @"idleTime", nil];
[readTask setArguments:arguments];

NSPipe *pipe = [NSPipe pipe];
[readTask setStandardOutput:pipe];

NSFileHandle *file = [pipe fileHandleForReading];

[readTask launch];
[readTask release];

NSData *data = [file readDataToEndOfFile];

NSString *originalValue = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

现在您拥有了屏幕保护程序空闲时间的原始值。伟大的!不要失去那个。现在,您必须设置新值:

NSTask *writeTask = [[NSTask alloc] init];
[writeTask setLaunchPath:@"/usr/bin/defaults"];

NSArray *arguments = [NSArray arrayWithObjects:@"-currentHost", @"write", @"com.apple.screensaver", @"idleTime", @"0", nil];
[writeTask setArguments:arguments];

[writeTask launch];
[writeTask release];

还有中提琴!您刚刚禁用了屏幕保护程序。要重新启用它,只需再次使用第二个代码块,但传入 OriginalValue 作为最后一个数组对象而不是 @"0",如下所示:

NSArray *arguments = [NSArray arrayWithObjects:@"-currentHost", @"write", @"com.apple.screensaver", @"idleTime", originalValue, nil]

享受!
Billy

P.S.:最后一件事,您可能会想保存 NSTask 对象以重新使用它们,但不要这么做。它们只能运行一次,因此每次您想要执行此操作时都必须创建新的。

First, you need to save the current setting, so you can put it back the way it was before you turned it off:

NSTask *readTask = [[NSTask alloc] init];
[readTask setLaunchPath:@"/usr/bin/defaults"];

NSArray *arguments = [NSArray arrayWithObjects:@"-currentHost", @"read", @"com.apple.screensaver", @"idleTime", nil];
[readTask setArguments:arguments];

NSPipe *pipe = [NSPipe pipe];
[readTask setStandardOutput:pipe];

NSFileHandle *file = [pipe fileHandleForReading];

[readTask launch];
[readTask release];

NSData *data = [file readDataToEndOfFile];

NSString *originalValue = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

So now you have the original value for the screensaver's idleTime. Great! Don't lose that. Now, you have to set the new value:

NSTask *writeTask = [[NSTask alloc] init];
[writeTask setLaunchPath:@"/usr/bin/defaults"];

NSArray *arguments = [NSArray arrayWithObjects:@"-currentHost", @"write", @"com.apple.screensaver", @"idleTime", @"0", nil];
[writeTask setArguments:arguments];

[writeTask launch];
[writeTask release];

And viola! You've just disabled the screensaver. To re-enable it, just use the second block of code again, but pass in originalValue as the last array object rather than @"0", like so:

NSArray *arguments = [NSArray arrayWithObjects:@"-currentHost", @"write", @"com.apple.screensaver", @"idleTime", originalValue, nil]

Enjoy!
Billy

P.S.: One last thing, you may be tempted to save the NSTask objects to re-use them, but don't. They can only be run once, so you'll have to create new ones every time you want to do this.

蛮可爱 2024-09-11 22:41:55

对于任何正在寻找如何执行此操作(就像我一直在做的那样)并且不想浪费时间编辑首选项文件的人来说,Apple 有一种适当的方法可以在应用程序运行时阻止屏幕保护程序启动。

技术问答 QA1160:防止睡眠

希望这会有所帮助。

For anyone searching for how to do this (like I have been doing) and don't want to mess around with editing the preference files, Apple has a proper method to stop the screen saver from starting up while your application is running.

Technical Q&A QA1160: Preventing sleep

Hope this helps.

沒落の蓅哖 2024-09-11 22:41:55

我最终做的是直接读取 com.apple.screensaver 首选项文件并修改idleTime 和askForPassword 值以使它们为零。一个简单的 CFPreferencesSynchronize 就一切顺利!

What I ended up doing was directly reading the com.apple.screensaver preference file and modifying the idleTime and askForPassword values so that the are zero. A simple CFPreferencesSynchronize and all was well!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文