Tuesday, August 14, 2007

Running an application as root in Objective-C

Okay, so if you look back into the past a bit on the Blog history, I was having problems with this type of thing. I had an application that needed to access files belonging to root. Anyway, after 2 1/2 hours reading documentation, I found this code snippet.

AuthorizationRef authorizationRef;

AuthorizationExecuteWithPrivileges(authorizationRef, "/Path/To/Application.app/Contents/MacOS/Application", kAuthorizationFlagDefaults, nil, nil);

Now, this will run any application as a root user so use it wisely. Just make sure, if you are trying to run a .app file, you need to continue into it's contents and run the actual application. That's represented above. Just replace both "Application" instances with your application's name.

After the call is made, the OS will launch the application specified with root user privileges. It will require that you authenticate as an admin before the application is launched.

No comments: