Launch containing app from system extension

Hi, I’m using network system extension and I wanted to launch the containing app from the extension. I'm using NSWorkspace.shared.openApplication(at applicationURL: URL, configuration: NSWorkspace.OpenConfiguration) async throws -> NSRunningApplication and sometimes it works but sometimes I gets those errors:

-[_LSRemoteOpenCall invokeWithError:]: XPC error encountered talking to CSUIA: <error: 0x7fff9793e9a0> { count = 1, transaction: 0, voucher = 0x0, contents =
"XPCErrorDescription" => <string: 0x7fff9793eb08> { length = 18, contents = "Connection invalid" }
}

error Optional(Error Domain=NSCocoaErrorDomain Code=256 "The application “APP_NAME” could not be launched because a miscellaneous error occurred." UserInfo={NSURL=file:/Applications/APP_NAME.app/, NSLocalizedDescription=The application “APP_NAME” could not be launched because a miscellaneous error occurred., NSUnderlyingError=0x7fcf005634d0 {Error Domain=NSOSStatusErrorDomain Code=-10822 "kLSServerCommunicationErr: The server process (registration and recent items) is not available" UserInfo={_LSLine=354, _LSFunction=-[_LSRemoteOpenCall invokeWithError:]}}})

Want is the best practice to open the containing application from the system extension. Thanks

SYSEXTs don't run in a user session. So what you want to do is not possible, except maybe indirectly via launchctl asuser <UID> ... -- not sure if this would actually use the console session though. There's another indirect way too: launch agents, but then why not just have your main app be a launch agent?

What bbergstrand said [1] plus…

You wrote:

I’m using network system extension and I wanted to launch the containing app from the extension.

To what end?

A system extension is effectively a thin wrapper around a launchd daemon. Thus, your goal of launching the containing app doesn’t make sense in general. Which user session would it launch into? A system extension can be running when no user is logged in. And it can also be running when multiple user’s are logged in.

Daemon developer have been asking this question since the dawn of Mac OS X. I encourage you to read through Technote 2083 Daemons and Agents, which explains how macOS’s execution contexts fit together. If your daemon needs to perform operations in GUI login sessions, you’ll need an agent running in each such session. See The Perils of the Monolith section of TN2083.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Except for this bit:

except maybe indirectly via launchctl asuser <UID> ...

I strongly recommend that you not try anything like this. Attempting to switch user sessions is very likely to end in tears.

Launch containing app from system extension
 
 
Q