import SwiftUI
struct ContentView: View {
@State private var username: String = ""
@State private var password: String = ""
@State private var connectionStatus: String = ""
@discardableResult func shell(_ command: String, withPassword password: String) -> (String?, Int32) {
// Erstelle neuen Prozess und lege den Pfad fest
let task = Process()
task.launchPath = "/bin/bash"
// Konstruiere das Kommando zur Nutzung von sudo mit Passwort
let sudoCommand = "echo \(password) | sudo -S \(command)"
// "-c" Argument lässt nachfolgenden Befehl als Bash-Befehl ausführen
task.arguments = ["-c", sudoCommand]
// Pipe für Ausgabe und Fehlermeldung
let pipe = Pipe()
task.standardOutput = pipe
task.standardError = pipe
// Starte Prozess
task.launch()
// Lese gesamte Ausgabe
let data = pipe.fileHandleForReading.readDataToEndOfFile()
// Konvertiere Ausgabe in einen String, falls möglich
let output = String(data: data, encoding: .utf8)
task.waitUntilExit()
return (output, task.terminationStatus)
}
var body: some View {
VStack {
Text("Connect Network Drive")
.font(.title)
.bold()
.padding()
TextField("Username", text: $username)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
SecureField("Password", text: $password)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
Button(action: {
runScript()
}) {
Text("Connect").font(.body)
}
.padding()
Text(connectionStatus)
.foregroundColor(.red)
.padding()
Spacer()
}
.padding()
}
func runScript() {
// Pfad zum Mountpunkt und zum Netzlaufwerk
let mountPoint = "~/Desktop/DriveH"
let networkPath = "//\(username):\(password)@server/Pfad" // Passen Sie den Netzwerkpfad an
// Erstellen des Verzeichnisses, falls es nicht existiert
let mkdirCommand = "mkdir -p \(mountPoint)"
let mountCommand = "mount_smbfs \(networkPath) \(mountPoint)"
// Führen Sie den Befehl zum Erstellen des Verzeichnisses aus
let mkdirResult = shell(mkdirCommand, withPassword: password)
if mkdirResult.1 != 0 {
connectionStatus = "Failed to create mount point: \(mkdirResult.0 ?? "Unknown error")"
return
}
// Führen Sie den Befehl zum Mounten des Netzlaufwerks aus
let mountResult = shell(mountCommand, withPassword: password)
if mountResult.1 == 0 {
connectionStatus = "Network drive connected successfully."
} else {
connectionStatus = "Failed to connect network drive: \(mountResult.0 ?? "Unknown error")"
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
我正在尝试通过 Swift UI 安装网络驱动器。但不幸的是我只得到以下回复“不允许操作”,有人可以帮助我吗? 我已经尝试将其作为“sudo”运行,但这也无法正常工作。但也可能是我编程错误。 我已经尝试通过 bash 脚本安装它,以便按钮调用 bash 脚本,但不幸的是,这导致了相同的错误消息。
不允许在沙箱环境中运行shell脚本。嗯,在某些情况下是可能的,但
Process
是严格禁止的。
但是,如果您在应用程序容器中的某个位置(例如在
NetFSMountURLAsync
文件夹中)指定挂载点,则 NetFS
框架中的 Application Support
确实可以在沙箱中工作。
重要提示:您必须在沙箱设置中启用
Outgoing Connections (Client)
。
这是一个例子
import NetFS
var requestID : AsyncRequestID?
let url = URL(string: "smb://MyServer.local/myvolume")!
let userName = "user"
let password = "pass"
let mountPoint = URL.applicationSupportDirectory
NetFSMountURLAsync(url as CFURL,
mountPoint as CFURL,
userName as CFString,
password as CFString,
nil,
nil,
&requestID,
DispatchQueue.global()) { (status, requestID, mountpoints) in
if status != 0 && status != -128 {
print("Shared Volume mount at \(url.path) failed with status: \(status)")
}
}