I'm trying to remove a file form the ~/Library/PreferencePanes folder, and this cannot be done with normal privileges. Usually, if you need to delete any file from the ~/Library folder the system will ask for your username and password.
The current way I do this is with
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error]
but this keeps on returning an error ([error localizedDescription]
):
theAppName.prefPane couldn’t be removed because you don’t have permission to access it.
What logic and code would I need to use in order to gain these elevated privileges needed for deleting files?
Thanks!
You should go through the Authorization Services Programming Guide. You will need to use AuthorizationExecuteWithPrivileges
.
The only method I could find and which worked for me was to create a command line tool in Xcode. Install the tool as a Launchd Job using SMJOBBLESS and then use XPC connection to talk to it. Follow the below EvenBetterAuthorization sample to understand how all of the above works.
来源:https://stackoverflow.com/questions/6267197/osx-how-to-programmatically-delete-a-file-with-elevated-privileges