开发者

multiple AuthorizationExecuteWithPrivileges

开发者 https://www.devze.com 2023-03-08 06:25 出处:网络
My application needs authentication to write to the hosts file. I can do this using the following bit of code, that I call. My problem is that when sometimes the user will need to make this change mor

My application needs authentication to write to the hosts file. I can do this using the following bit of code, that I call. My problem is that when sometimes the user will need to make this change more than once in that instance of the program - the warning dialog asking for the password only appears the 开发者_开发技巧first time this is called, and even though the function is called again later, the password request does not show. Can anyone shed any light into this? thanks.

- (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);
}


If you want to force re-authentication you should call

    status = AuthorizationFree (authorizationRef, kAuthorizationFlagDestroyRights);

after AuthorizationExecuteWithPrivileges

0

精彩评论

暂无评论...
验证码 换一张
取 消