在 Cocoa 和 Objective-C 中使用 sudo 命令?
从 Objective-C/Cocoa 中运行 $sudo touchfolder_name
或 $sudo rm
的正确方法是什么?我正在更改和移动一些文件,需要提升权限。任何代码示例将不胜感激。谢谢。
What is the proper way to run something like $sudo touch folder_name
or $sudo rm
from within Objective-C/Cocoa? I'm changing and moving around a few files and need elevated privileges. Any code sample would be greatly appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种方法是使用
AuthorizationExecuteWithPrivileges()
,但在正常情况下不鼓励这样做。据我所知,它主要是供安装人员使用的。类似于:
并且您添加了对
err
的适当检查...请参阅授权文档。
One way to do it is
AuthorizationExecuteWithPrivileges()
, but that's discouraged for the normal course of things. Mainly it's for installers, I gather.Something like:
And you add appropriate checking of
err
...See the Authorization documentation.
有几种方法可以做到这一点。您选择哪一个取决于您想要做什么。最简单且最不安全的方法是在您从代码中调用的额外帮助工具上简单地设置 s 位,并执行需要管理员权限的操作。查看 BetterAuthorizationSample 用于执行特权代码的最奇特和最复杂的方式。
授权服务编程指南给出了你所需要的一切。
There are several ways to do this. Which one you choose depends on what you want to do. The simplest and unsafest way is to simple set the s-bit on an extra helper tool that you call from your code and does what needs admin rights. Take a look at the BetterAuthorizationSample for the most fancy and complicated way of executing privileged code.
The Authorization Services Programming Guide gives you all you need.