1

在我的 Cocoa 应用程序中,我想访问和更改计算机的屏幕锁定超时设置。在系统偏好设置中更改它不需要用户输入管理员密码。

系统偏好截图 - 屏幕锁定超时

不幸的是,我在文档中找不到任何信息,而且我不确定我应该研究什么主题(安全设置/prefPane 编程)。
任何帮助,将不胜感激。

4

2 回答 2

1
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/Users/new/Library/Preferences/com.apple.screensaver.plist"];
[plistDict setObject:@"1" forKey:@"askForPassword"];
[plistDict setObject:@"3600" forKey:@"askForPasswordDelay"];
[plistDict writeToFile:@"/Users/new/Library/Preferences/com.apple.screensaver.plist" atomically:YES];  

或从终端

defaults write com.apple.screensaver askForPasswordDelay 5
于 2012-05-23T12:43:20.193 回答
1

上面的答案显然适用于某些人,但在 10.8 上,如果您使用 FileVault,它会失败。该设置将保持不变,但在您启动“系统偏好设置”之前它不会真正生效。幸运的是,完成后有一种方法可以“触摸”设置:

- (void)touchSecurityPreferences;
{
    NSAppleScript *kickSecurityPreferencesScript = [[[NSAppleScript alloc] initWithSource: @"tell application \"System Events\" to tell security preferences to set require password to wake to true"] autorelease];
    [kickSecurityPreferencesScript executeAndReturnError:nil];
}

编辑原来这仅适用于从非零设置到零设置。我认为这是一个安全问题。反过来,启动系统偏好设置是唯一的方法。

编辑 2如果您想这样做,这里是启动系统偏好设置的代码:

- (void)launchAndQuitSecurityPreferences;
{
    // necessary for screen saver setting changes to take effect on file-vault-enabled systems when going from a askForPasswordDelay setting of zero to a non-zero setting
    NSAppleScript *kickSecurityPreferencesScript = [[[NSAppleScript alloc] initWithSource:
                                                     @"tell application \"System Preferences\"\n"
                                                     @"     tell anchor \"General\" of pane \"com.apple.preference.security\" to reveal\n"
                                                     @"     activate\n"
                                                     @"end tell\n"
                                                     @"delay 0\n"
                                                     @"tell application \"System Preferences\" to quit"] autorelease];
    [kickSecurityPreferencesScript executeAndReturnError:nil];
}
于 2013-05-03T23:01:17.240 回答