我在网上搜索了很多东西,找到了一种在我的NSTask中获得root权限的简单方法,但我只找到了用Objective-C编写的旧文章。但是我的Xcode项目是用Swift编写的:x有没有办法解决这个问题来运行带有Root Privilegs的NSTask? :)我知道我必须使用AppleScript,STPrivilegedTask或Apple的BetterAuthorizationSample之类的东西,但我发现的所有内容都是用Objective-C编写的......
我的想法是使用命令createinstallmedia
创建一个可以启动存储介质的应用程序:p完全正常工作唯一的问题是该命令需要root权限,为了测试我只需打开我的终端并登录为Root但这不是解决这个问题的最佳解决方案:D所以请帮助我! :○
我的代码唯一遗漏的是获取root权限的代码:
import Cocoa
class ViewController: NSViewController {
var Task = NSTask()
var openPanel = NSOpenPanel()
var workingpath_createinstallmedia : String!
var workingpath_medium : String!
var workingpath_application : String!
var volume_flag = "--volume"
var applicationpath_flag = "--applicationpath"
var nointeraction_flag = "--nointeraction"
var commandpath : String!
var postcommand : [String]!
var DirectoryOS : NSURL!
var DirectoryMedium : NSURL!
@IBOutlet weak var PathControlOS: NSPathControl!
@IBOutlet weak var PathControlMedium: NSPathControl!
@IBOutlet weak var Outputlabel: NSTextField!
@IBAction func OSauswählen(sender: AnyObject) {
openPanel.canChooseDirectories = false
openPanel.canChooseFiles = true
openPanel.canCreateDirectories = false
openPanel.allowsMultipleSelection = false
if openPanel.runModal() == NSModalResponseOK {
DirectoryOS = openPanel.URLs[0] as NSURL
PathControlOS.objectValue = DirectoryOS
}
}
@IBAction func Mediumauswählen(sender: AnyObject) {
openPanel.canChooseDirectories = true
openPanel.canChooseFiles = false
openPanel.canCreateDirectories = false
openPanel.allowsMultipleSelection = false
if openPanel.runModal() == NSModalResponseOK {
DirectoryMedium = openPanel.URLs[0] as NSURL
PathControlMedium.objectValue = DirectoryMedium
}
}
@IBAction func starttask(sender: AnyObject) {
// edit Strings
// Createinstallmedia
workingpath_createinstallmedia = String(DirectoryOS)
workingpath_createinstallmedia = workingpath_createinstallmedia.stringByReplacingOccurrencesOfString("file://", withString: "")
workingpath_application = workingpath_createinstallmedia.stringByReplacingOccurrencesOfString("%20", withString: " ")
workingpath_createinstallmedia = workingpath_application + "/Contents/Resources/createinstallmedia"
// Medium
workingpath_medium = String(DirectoryMedium)
workingpath_medium = workingpath_medium.stringByReplacingOccurrencesOfString("file://", withString: "")
workingpath_medium = workingpath_medium.stringByReplacingOccurrencesOfString("%20", withString: " ")
// config Task Parameters
commandpath = workingpath_createinstallmedia
postcommand = [volume_flag,workingpath_medium,applicationpath_flag,workingpath_application,nointeraction_flag]
// commit the Parameters to the Task
Task.launchPath = commandpath
Task.arguments = postcommand
// start Task
Task.launch()
Task.waitUntilExit()
}
}
感谢您的帮助!!!
答案 0 :(得分:1)
我解决了在命令中使用sudo并向用户询问密码以直接提供给sudo,方法如下:
echo "passwordhere" | sudo -S command --arguments
并使用sh shell可执行文件来运行该进程,使用此方法我成功创建了一个应用程序来创建名为TINU的可启动mac os安装媒体,但是在处理用户密码时需要小心,我选择了我的应用程序开源让其他人知道它对这类需要用户密码的任务有什么作用,但是在mac os上你应该创建专门的程序来执行这种特权任务,然后使用系统apis启动它们,为你管理身份验证并让专业程序完成他的工作。