Update:. To update the people who are still using this answer for reference, BLAuthentication uses an old and very unmanageable function called AuthorizationExecuteWithPriviledges , which runs counter to the current security paradigm and is outdated (and was for a while). You can still use it technically, but if you are developing for Mac OS X Lion, you can more than use the ServicesManagement infrastructure, which allows you to run privileged code as an auxiliary tool.
For help on how to create and run a privileged helper tool, take a look at one of my questions, Writing a privileged helper tool using SMJobBless () .
There is no real easy way to authorize NSFileManager , so you should study the standard mv and cp tools running under administrator authentication with the BLAuthentication class. Unfortunately, the site of the original author does not work, but you can easily find copies of the class floating on Google (I can also download a copy for you if you wish).
With BLAuthentication , what you are trying to do is something like this:
#define MOVE @"/bin/mv" if (![[BLAuthentication sharedInstance] isAuthenticated:MOVE]) { [[BLAuthentication sharedInstance] authenticate:MOVE]; } NSArray *arguments = [NSArray arrayWithObjects:@"location1", @"location2", nil]; [[BLAuthentication sharedInstance] executeCommand:MOVE withArgs:arguments];
The above code will ask the user for an administrator password and verify the authenticity of the program by default after five minutes.
Attention
Of course, always be careful with system files! Avoid moving or manipulating them whenever possible, especially if your program runs on another computer (if something goes wrong, you will be blamed)!
Itai ferber
source share