我正在为使用 Swift 的运行iOS 12的越狱设备制作一个iOS应用。
最近我问了一个问题,我自己回答了这个问题,在其中我询问了通往run command line tasks with Swift in iOS的方法。
但是,正如您所看到的,我还可以使用NSTask来完成任务,但是在运行该应用程序时会崩溃。
基本上,我有一个NSTask.h
file,可以让我在Swift中使用NSTask。
因此,要启动任务,我做了以下功能:
func task(launchPath: String, arguments: String...) {
let task = NSTask.init()
task?.setLaunchPath(launchPath)
task?.arguments = arguments
// Create a Pipe and make the task
// put all the output there
let pipe = Pipe()
task?.standardOutput = pipe
// Launch the task
task?.launch()
task?.waitUntilExit()
}
并调用如下函数:
task(launchPath: "/usr/bin", arguments: "git clone https://github.com/lz4/lz4.git")
问题是,当我运行该应用程序时,它崩溃并显示以下错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Couldn't posix_spawn: error 13'
该如何解决该错误?
谢谢。
编辑:顺便说一句,我忘了提及我使用桥接标题访问NSTask.h
文件。
答案 0 :(得分:1)
启动路径必须是可执行文件的完整路径,而不是包含可执行文件的目录。同样,应将命令参数作为单独的参数而不是一个字符串提供。 (请注意,在启动进程之前,没有涉及shell来解析命令并分隔参数。)因此,调用应类似于
task(launchPath: "/usr/bin/git", arguments: "clone", "https://github.com/lz4/lz4.git")