[英]Execute terminal command as root from Swift Code
下面的代码允许我创建 ui 并在没有 root 权限的情况下运行终端命令。 我的目标是从需要 root 权限的 swift 代码运行关闭命令。 任何人都可以帮我解决这个问题吗?
//
// ContentView.swift
// Created by laughinggg on 8/27/20.
// Copyright © 2020 laughinggg. All rights reserved.
//
import SwiftUI
struct windowSize {
let minWidth : CGFloat = 500
let minHeight : CGFloat = 200
let maxWidth : CGFloat = 200
let maxHeight : CGFloat = 250
}
struct ContentView: View {
@State var message = "+"
@State var isRunning = false
var body: some View {
VStack {
Text("HELLO World!!!")
.font(.largeTitle)
.padding()
HStack {
TextField("Message", text: $message)
.padding(.leading)
Button(action: {
let exe = URL(fileURLWithPath: "/sbin/shutdown")
self.isRunning = true
try! Process.run(exe,
arguments: [self.message],
terminationHandler: { _ in self.isRunning = false })
}) {
Text("Say")
}.disabled(isRunning)
.padding(.trailing)
}
}.frame(maxWidth:500, maxHeight: 500)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
看看这个项目: https : //github.com/sveinbjornt/STPrivilegedTask
有一个名为AuthorizationExecuteWithPrivileges
的函数已被长期弃用但仍然有效。
Apple 的文档: https : //developer.apple.com/documentation/security/1540038-authorizationexecutewithprivileg
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.