我想使用swift
在macOS上创建加密的.dmg我还没有找到hdiutil的实现,所以我不得不使用终端命令。
不幸的是,我不知道如何使用管道(应该用于苹果文档)
我想要触发的命令如下:
printf 'password' | hdiutil create -encryption AES-256 -volname backupname -stdinpass -size 1GB backupname.dmg
代码,我试过的是以下(没有用):
func shell(_ args: String...) -> Int32 {
let task = Process()
let inputpipe = Pipe()
task.standardInput = inputpipe
//1. try: inputpipe.fileHandleForReading.write("password".data(using: .utf8)!)
//2. try: inputpipe.fileHandleForWriting.write("password".data(using: .utf8)!)
/*3. try: inputpipe.fileHandleForWriting.writeabilityHandler = {
pipe in
let inputString = "password"
let data = inputString.data(using: .utf8)!
pipe.write(data)
}*/
task.launchPath = "/usr/bin/env"
task.arguments = args
task.launch()
task.waitUntilExit()
return task.terminationStatus
}
print(shell("hdiutil", "create", "-encryption", "AES-256", "-volname", "backupname", "-stdinpass", "-size", "1MB", "~/Documents/backupname.dmg"))
如何使用Process()