多个AuthorizationExecuteWithPrivileges

时间:2011-05-24 17:22:52

标签: objective-c macos

我的应用程序需要身份验证才能写入hosts文件。我可以使用下面的代码来执行此操作。我的问题是,当有时用户需要在该程序实例中多次进行此更改时 - 仅在第一次调用此密码时才会出现要求输入密码的警告对话框,即使稍后再次调用该函数,密码请求未显示。任何人都可以对此有所了解吗?感谢。

- (void)someFunction {
    AuthorizationRef authorizationRef;
    OSStatus status;
    status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
         kAuthorizationFlagDefaults, &authorizationRef);

    //Run the tool using the authorization reference
    char *tool = "/bin/mv";
    char *args[] = { "-f", "/tmp/hosts", "/etc/hosts" };
    FILE *pipe = NULL;
    status = AuthorizationExecuteWithPrivileges(authorizationRef,
           tool, kAuthorizationFlagDefaults, args, &pipe);
}

1 个答案:

答案 0 :(得分:2)

如果您想强行重新认证,请致电

    status = AuthorizationFree (authorizationRef, kAuthorizationFlagDestroyRights);
AuthorizationExecuteWithPrivileges

之后