我正在开发一个将更新 /etc/hosts 的应用程序。为此,我发现评估权限的首选方法是通过 SMJobBless 安装帮助工具。以下代码类型的作品:
BOOL result = NO;
AuthorizationItem authItem = { kSMRightBlessPrivilegedHelper, 0, NULL, 0 };
AuthorizationRights authRights = { 1, &authItem };
AuthorizationFlags flags = kAuthorizationFlagDefaults |
kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagPreAuthorize |
kAuthorizationFlagExtendRights;
AuthorizationRef authRef = NULL;
CFErrorRef error = NULL;
OSStatus status = AuthorizationCreate(&authRights, kAuthorizationEmptyEnvironment, flags, &authRef);
if (status == errAuthorizationSuccess) {
result = SMJobBless(kSMDomainSystemLaunchd, (CFStringRef)@"com.fictitiousnonsense.MeddoHelper", authRef, &error);
} else {
NSLog(@"Failed to authorize");
}
if (error != NULL) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"I think it worked");
}
根据文档,我的帮助工具 com.fictitiousnonsense.MeddoHelper 安装到 /Library/PrivilegedHelperTools/com.fictitiousnonsense.MeddoHelper 并且它的 plist 文件安装到 /Library/LaunchDaemons。问题是这会出现在我的控制台中:
9/24/12 11:04:41.237 PM launchdadd[9082]: Could not open /Library/PrivilegedHelperTools/com.fictitiousnonsense.MeddoHelper (open() error 2: No such file or directory)
9/24/12 11:04:41.237 PM launchdadd[9082]: FAILURE: The path /Library/PrivilegedHelperTools/com.fictitiousnonsense.MeddoHelper does not exist on-disk.
我可以从终端手动运行助手并且它可以工作。为什么launchd不运行它?我已经运行了 Apple 提供的 SMJobBlessApp 示例应用程序,它运行良好,但我不会,而且我找不到不同的。
作为参考,整个代码在这里:https ://github.com/varikin/meddo 。