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 don't have permission to delete the file Control-Click them and select Get Info for them and then scroll down the window that opens to the section headed “Sharing and Permissions”. In this menu make sure that the user you're currently signed in as has permission to read and write these files.
To permanently delete a file or folder, bypassing the Trash, press Option + Command + Delete keys. To remove a file a folder to the Trash, press Command + Delete keys. To empty your Trash, use Command + Option + Shift + Delete.
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://developer.apple.com/library/mac/samplecode/EvenBetterAuthorizationSample/Listings/Read_Me_About_EvenBetterAuthorizationSample_txt.html#//apple_ref/doc/uid/DTS40013768-Read_Me_About_EvenBetterAuthorizationSample_txt-DontLinkElementID_17
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With