swiftmacosmacos-system-extension

Pass Argument From App Delegate to System Extension, Mac OS


I am creating a system extension and bundling it in a Mac app on Mac OS Catalina. I have had success building the app and requesting activation of the extension from my application delegate (Swift). My System Extension can successfully run.

My question is that I need to somehow pass additional arguments from the app delegate that requests activation of the extension to the main function of my system extension. Specifically, I am trying to pass the username of the user that ran the application. Is there a way to pass additional arguments from the delegate to the extension?

I cannot get the current user from within the system extension because the extension runs as root, always. So the fetching the current user will return root.

Relevant code from the app delegate:

func applicationDidFinishLaunching(_ aNotification: Notification) {
   
    let ID = "com...."
    let req = OSSystemExtensionRequest.activationRequest(forExtensionWithIdentifier: ID,
                                         queue: DispatchQueue.main).                                         
    req.delegate = self
    

    /*some how pass this arguments to extension*/
    let username = NSFullUserName()

    OSSystemExtensionManager.shared.extensionManager.submitRequest(req)                 
}

Main function of my system extension:

int main(int argc, const char* argv[]){
        //start.....extension
        //Need to receive arguments here
        dispatch_main();
}

If I could somehow receive the username passed into argv, that would be great. I am open to other solutions. The bottom line is I need someway to communicate this username value between the two.


Solution

  • If I understand your question correctly, then the answer depends heavily on what type of system extension you are developing. For example: