xcode NSTask:无法 posix_spawn:启动应用程序时出现错误 13

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38132589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:59:44  来源:igfitidea点击:

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

xcodeswiftmacosnstask

提问by Link14

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.

我的主 Swift 应用程序中有一个子应用程序。我做了它,所以在构建它时它会自动复制到主应用程序的资源文件夹中。这样,我希望能够从主应用程序启动子应用程序的实例。

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 Agentto YES. And in MainMenu.xib, I set the Visible at launchoption to no. That sub-app needs to do some work in the background and doesn't need any UI at all.

我不知道去哪里找,找什么。我不知道我做错了什么。我想知道这个问题是否与子应用程序本身有关。该子应用程序现在是空的。我设置Application is AgentYES。在 MainMenu.xib 中,我将Visible at launch选项设置为 no。该子应用程序需要在后台做一些工作,根本不需要任何 UI。

Thanks !

谢谢 !

回答by NSGod

Don't use NSTaskfor this, use NSWorkspace:

不要NSTask为此使用,请使用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 NSRunningApplicationwhich represents the task.

在上面的代码中,为简洁起见,我忽略了openURL()命令的结果,但实际上它可以返回一个NSRunningApplication代表任务的实例。

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

要跟踪您启动的助手应用程序的实例,您可以NSRunningApplication在适当类型的集合类中保留对此的引用,并在时机成熟时调用其terminate()方法。