我在網上搜索了很多,在我的NSTask中找到了獲取root權限的簡單方法,但我只找到了用Objective-C編寫的舊文章。但是我的Xcode項目是用Swift編寫的:x有沒有辦法解決這個問題來運行具有Root Privilegs的NSTask? :)我知道我必須使用像AppleScript,STPrivilegedTask或BetterAuthorizationSample從Apple的東西,但我發現所有是Objective-C寫...NSTask在Swift中的根權限
我的想法是創建一個應用程序,使存儲介質可啓動使用命令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()
}
}
感謝您的幫助!
您是否嘗試過翻譯*其中的任何一個*到Swift? –
當然,我試圖用AppleScript和一個名爲SMJobBless()的函數來解決這個問題。但沒有爲我工作... – Joh
你找到了解決方案?我在swift中編寫安裝程序,我很想獲得使用具有root特權的進程的權限。 – jl303