简体   繁体   中英

How can I run an embedded binary in my OSX application?

I have an application which needs to run an external program.

I have included this program as an Embedded Framework . When archived, it appears at the location %AppRoot%/Contents/Frameworks/MyExternalApplication.

How can I run this program from my app's code without using an absolute path? I'd like to run in both release and debug.

Thank You!

You can run the executable by using an NSTask object or the posix_spawn() function.

You can use the NSBundle class to find the absolute path of the executable. See this article for details.

Notes:

  1. That is not the correct place within the app bundle to place the executable; I believe it should be in Contents/MacOS , as documented here .
  2. If the main app is sandboxed (a requirement if you want to put the app on the Mac App Store) then an entitlement is required that will be embedded into the external executable so that it inherits the app's sandbox. See this question for details.
  3. Once you've done 2. you will notice that the executable will no longer run from the command line, but that's nothing to worry about, as it will run from within your app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM