我正在编写一个应用程序,我希望自定义首选项窗口在允许任何更改之前询问管理员密码(但从不存储它)。现在我一直在使用这个代码片段:
OSStatus status;
AuthorizationRef authorizationRef;
// AuthorizationCreate and pass NULL as the initial
// AuthorizationRights set so that the AuthorizationRef gets created
// successfully, and then later call AuthorizationCopyRights to
// determine or extend the allowable rights.
// http://developer.apple.com/qa/qa2001/qa1172.html
status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &authorizationRef);
if (status != errAuthorizationSuccess)
{
NSLog(@"Error Creating Initial Authorization: %d", status);
return status;
}
// kAuthorizationRightExecute == "system.privilege.admin"
AuthorizationItem right = {kAuthorizationRightExecute, 0, NULL, 0};
AuthorizationRights rights = {1, &right};
AuthorizationFlags flags = kAuthorizationFlagDefaults | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagPreAuthorize | kAuthorizationFlagExtendRights;
// Call AuthorizationCopyRights to determine or extend the allowable rights.
status = AuthorizationCopyRights(authorizationRef, &rights, NULL, flags, NULL);
if (status != errAuthorizationSuccess)
{
NSLog(@"Copy Rights Unsuccessful: %d", status);
}
return status;
效果很好,显示了一个标准的操作系统密码对话框,要求输入管理员密码。首选项 nib 中的各种控件根据返回的status
. 但是,现在我正在尝试对应用程序进行沙箱处理,并且此代码始终返回errAuthorizationDenied
. 我已经查看了文档AuthorizationCopyRights
,AuthorizationCreate
但我看不到在沙盒环境中使用它们的参考。
我尝试过各种不同的方法,AuthorizationFlags flags
但结果总是一样的。有没有办法修改上述代码以在沙盒中工作,或者这些天要求管理员密码只是一个禁忌?