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
精彩评论