Swift中NSTask的根权限

4

我在网上搜寻了许多内容,想要找到一种简单的方法来获取NSTask中的root权限,但是我只找到了一些用Objective-C编写的旧文章。但我的Xcode项目是用Swift编写的,在运行带有Root权限的NSTask时,是否有解决此问题的方法?我知道我必须使用类似于AppleScript、STPrivilegedTask或者Apple的BetterAuthorizationSample,但我找到的所有内容都是用Objective-C编写的...

我的想法是创建一个应用程序,利用命令createinstallmedia使存储介质可启动。所有工作都进行得很完美,唯一的问题是该命令需要root权限。为了测试,我只需打开终端并登录为Root,但这不是解决此问题的最佳方案。请帮帮我!

我的代码中唯一缺失的就是获取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()

    }
}

感谢您的帮助!!!(这是需要翻译的内容)

你尝试过将它们中的任何一个翻译成Swift吗? - Code Different
当然,我尝试使用AppleScript和名为SMJobBless()的函数来解决这个问题。 但是对我来说都没用... - Joh
你找到解决方案了吗?我正在使用Swift编写安装程序,希望能够获得使用具有root权限的进程的访问权限。 - jl303
1个回答

0

我通过在命令中使用sudo并要求用户输入密码直接提供给sudo来解决问题,具体如下:

echo "passwordhere" | sudo -S command --arguments

使用sh shell可执行文件运行进程,我成功地创建了一个名为TINU的应用程序来创建可引导的Mac OS安装媒体,但在处理用户密码时需要小心。我选择将我的应用程序开源,让其他人知道它在处理需要用户密码的任务时所做的事情,但在Mac OS上,您应该创建专门的程序来执行此类特权任务,然后使用系统API启动它们,这些API将为您管理身份验证,并让专门的程序完成其工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接