Search code examples
iosswiftjailbreaknstask

'NSInternalInconsistencyException', reason: 'Couldn't posix_spawn: error 13' Swift


I'm making an iOS app for jailbroken devices running iOS 12 with Swift.

Recently I made a question, which I answered myself, in which I asked for a way to run command line tasks with Swift in iOS.

But as you can see, I'm not done yet as I'm able to use NSTask, but it crashes while running the app.

Basically, I have a NSTask.h file that allows me to use NSTask with Swift.

So, to launch a task I made the following function:

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()
}

And call the function like this:

task(launchPath: "/usr/bin", arguments: "git clone https://github.com/lz4/lz4.git")

The problem is that when I run the app, it crashes and prints the following error:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Couldn't posix_spawn: error 13'

What can I do to fix this error?

Thanks in advance.

EDIT: By the way, I forgot to mention I access the NSTask.h file using Bridging Headers.


Solution

  • The launch path must be the full path of the executable, not the directory containing the executable. Also the command arguments should be provided as separate arguments, not as one string. (Note that there is no shell involved which parses the command and separates the arguments before starting the process.) Therefore the call should be something like

    task(launchPath: "/usr/bin/git", arguments: "clone", "https://github.com/lz4/lz4.git")