Cocoa - 获取NSFileManager的根访问权限 [英] Cocoa - Gaining Root Access for NSFileManager

查看:1068
本文介绍了Cocoa - 获取NSFileManager的根访问权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我需要移动系统文件与NSFileManager在我的应用程序,我似乎没有根访问。什么是最简单的方式去获得这个特权?我已经查看了由Apple提供的BetterAuthorizationSample代码,并且我似乎不能如何让NSFileManager运行其任务一旦得到用户的批准。



感谢,



Kevin

解决方案

更新:要更新仍然使用此答案的人员以供参考, BLAuthentication 使用一个旧的,非常不推荐的函数 AuthorizationExecuteWithPriviledges ,在工作时,违反了现代安全模式,并且已被弃用(已经有一段时间了) 。你仍然可以使用它,技术上,但是如果你正在开发的Mac OS X Lion,你是非常欢迎使用ServicesManagement框架,允许你运行代码作为一个帮助工具的特权。 p>

有关如何创建和启动特权帮助工具的参考,请查看我的一个问题,使用SMJobBless()编写特权帮助工具






没有真正容易的方法授权 NSFileManager ,所以你应该看看使用标准 mv cp 工具使用 BLAuthentication 类在管理员身份验证下运行。很抱歉,原作者的网站已关闭,但您可以轻松找到在Google上浮动的课程副本(如果您愿意,我也可以为您上传副本)。






使用 BLAuthentication ,你要做的事情就像这样:

  #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];

上述代码将提示用户输入管理员密码,并根据默认时间限制验证程序5分钟。






警告

当然, !避免在可能的情况下移动或操作它们,特别是如果你的程序要在别人的计算机上运行(如果出错了,你会被指责)。


So I need to move system files with NSFileManager in my application and I don't seem to have root access. What would be the easiest way to go about gaining this privilege? I have looked into the BetterAuthorizationSample code provided by Apple, and I don't seem how I could have the NSFileManager run its task once its been given approval by the user. If anyone could help me out it would be great!

Thanks,

Kevin

解决方案

Update: To update people still using this answer for reference, BLAuthentication makes use of an old, and highly unrecommended function called AuthorizationExecuteWithPriviledges that, while working, goes against the modern security paradigm, and is deprecated (and has been for a while). You're still allowed to use it, technically, but if you're developing for Mac OS X Lion, you're more than welcome to use the ServicesManagement framework, that allows you to run code with privileges as a helper tool.

For reference on how to create and launch a privileged helper tool, take a look at one of my questions, Writing a Privileged Helper Tool with SMJobBless().


There's no real easy way to authorize NSFileManager, so you should look into using the standard mv and cp tools run under administrator authentication with the BLAuthentication class. Unfortunately, the original author's website is down, but you can easily find copies of the class floating around on Google (I can also upload a copy for you if you wish).


With BLAuthentication, what you are trying to do goes 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 code above will prompt the user for the administrator's password and authenticate the program for the default time limit of five minutes.


WARNING
Of course, always be careful with system files! Avoid moving or manipulating them when possible, especially if your program is going to be run on someone else's computer (if anything goes wrong, you're going to be blamed)!

这篇关于Cocoa - 获取NSFileManager的根访问权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆