Swift中NSTask的Root权限

问题描述 投票:3回答:1

我在网上搜索了很多,找到了一种在我的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()

    }
}

谢谢你的帮助!!!

xcode swift root osx-elcapitan nstask
1个回答
1
投票

我解决了使用sudo进入命令并向用户询问密码以直接提供给sudo,方法如下:

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

并使用sh shell可执行文件来运行该进程,使用此方法我成功创建了一个应用程序来创建名为TINU的可启动mac os安装媒体,但是在处理用户密码时需要小心,我选择让我的应用程序打开来源让其他人知道它对这类需要用户密码的任务有什么作用,但在mac os上你应该创建专门的程序来执行这种特权任务,然后使用系统apis启动它们,为你管理身份验证并让它完成他的工作的专门计划。

© www.soinside.com 2019 - 2024. All rights reserved.