osx - 如何以编程方式删除具有提升权限的文件?
我正在尝试从 ~/Library/PreferencePanes 文件夹中删除文件,但使用普通权限无法完成此操作。通常,如果您需要从 ~/Library 文件夹中删除任何文件,系统会询问您的用户名和密码。
我当前执行此操作的方法是使用
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error]
但这会继续返回错误([error localizedDescription]
):
theAppName.prefPane 无法删除,因为您无权访问它。
我需要使用什么逻辑和代码才能获得删除文件所需的提升权限?
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该浏览 授权服务编程指南。您将需要使用
AuthorizationExecuteWithPrivileges
。You should go through the Authorization Services Programming Guide. You will need to use
AuthorizationExecuteWithPrivileges
.我能找到并且对我有用的唯一方法是在 Xcode 中创建一个命令行工具。使用 SMJOBBLESS 安装该工具作为启动作业,然后使用 XPC 连接与其通信。请按照下面的 EvenBetterAuthorization 示例来了解上述所有内容的工作原理。
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
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