Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSTask : Couldn't posix_spawn: error 13 when launching app

I have a sub-app in my main Swift app. I made it so it's copied automatically in the Resources folder of the main app when building it. That way, I want to be able to launch an instance of the sub-app from the main app.

The thing is, I'm having an error that is hard to debug/find answers about.

Here is my code :

    let args = ["--args", "-admin_url", site.url, "-login", site.login, "-pass", site.password]
    let helperPath = (NSBundle.mainBundle().pathForResource("App Helper", ofType: "app"))!
    let task = NSTask.init()
    task.launchPath = helperPath
    task.arguments = args
    task.launch()

And the error :

[56490:7218926] Couldn't posix_spawn: error 13

I have no idea where to look, what to search for. I don't know what I'm doing wrong. I'm wondering if the issue is related to the sub-app itself. That sub-app is empty for now. I set Application is Agent to YES. And in MainMenu.xib, I set the Visible at launch option to no. That sub-app needs to do some work in the background and doesn't need any UI at all.

Thanks !

like image 667
Link14 Avatar asked Jun 30 '16 20:06

Link14


1 Answers

Don't use NSTask for this, use NSWorkspace:

let helperAppURL = NSBundle.mainBundle().URLForResource("App Helper",
                                      withExtension:"app")!

_ = try? NSWorkspace.sharedWorkspace().openURL(helperAppURL,
       options:[.Default],
   configuration:[NSWorkspaceLaunchConfigurationArguments :
            ["--args", "-admin_url", site.url, "-login",
             site.login, "-pass", site.password]])

In the above code, for brevity, I ignored the result of the openURL() command, but in reality it can return an instance of NSRunningApplication which represents the task.

To keep track of the instances of your helper app you launch, you could keep references to this NSRunningApplication in an appropriate kind of collection class, and when the time comes, call its terminate() method.

like image 114
NSGod Avatar answered Nov 10 '22 06:11

NSGod