Using sudo commands in Cocoa and Objective-C?

What is the correct way to run something like $sudo touch folder_nameor $sudo rmfrom Objective-C / Cocoa? I modify and move multiple files and need elevated privileges. Any sample code will be appreciated. Thank.

+3
source share
2 answers

One way to do this is AuthorizationExecuteWithPrivileges(), but it discourages the normal course of things. This is mainly for installers, I understand.

Sort of:

AuthorizationRef auth = NULL;
OSStatus err = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagInteractionAllowed, &auth);
err = AuthorizationExecuteWithPrivileges(auth, command, kAuthorizationFlagDefaults, args, NULL);

And you add the appropriate check err...

See the Authorization documentation .

+11
source

. , , . - s- , , , . BetterAuthorizationSample .

, .

+2

Source: https://habr.com/ru/post/1729646/


All Articles