12

Using the following steps, I'm able to embed an info.plist into a command-line tool.

http://developer.apple.com/library/mac/#documentation/Security/Conceptual/CodeSigningGuide/Procedures/Procedures.html

I know how to retrieve the plist file from a .bundle, but I'm not sure how to do the same in a single-file tool like I've got.

I've embedded the info.plist into the command-line tool so that I can store the version in it. Does anyone know how I can retrieve it at run-time so I can determine what version is running?

Thanks

bugfixr
  • 7,997
  • 18
  • 91
  • 144
  • 1
    possible duplicate of [Reading data linked to executable through -sectcreate __TEXT](http://stackoverflow.com/questions/7780789/reading-data-linked-to-executable-through-sectcreate-text) –  Oct 17 '11 at 19:16
  • Bavarious - it's not exactly a duplicate. Your referenced question requires using an external tool. I'd like to use straight objective-c code. – bugfixr Oct 17 '11 at 19:48
  • 1
    The answer in the previous question has been updated to show three methods to access an embedded plist. I’ve also added a link to BVPlistExtractor, which contains a function that returns the embedded plist of an arbitrary thin/fat Mach-O file. –  Oct 18 '11 at 20:24
  • I have found another solution: use `launchctl` tool: https://stackoverflow.com/a/56056655/3881449 – julia_v May 09 '19 at 09:46

1 Answers1

12

__info_plist is a "magic" section name that makes the following Just Work:

NSBundle *bundle = [NSBundle mainBundle];
id version = [bundle objectForInfoDictionaryKey: (NSString*) kCFBundleVersionKey];
NSLog(@"mainBundle.version = %@", version);

If you need to read a bundle embedded in a different executable than the one currently running, this answer by Bavarious from the comments has a more comprehensive list of approaches.

Community
  • 1
  • 1
millimoose
  • 39,073
  • 9
  • 82
  • 134
  • I had tried that myself, though I wasn't using the mainBundle, I was instead loading a bundle from disk and it would always return a nil for non .bundle files... I'll go back to that approach and make sure I wasn't doing something buggy instead. – bugfixr Oct 17 '11 at 19:46
  • @rscott: I'm guessing this only works with `NSBundle mainBundle` specifically for the currently running executable. Is there any reason why you can't use `mainBundle`? – millimoose Oct 17 '11 at 19:50
  • @rscott: Or, more specifically, are you trying to read an Info.plist embedded in the executable that's currently running, or from an arbitrary executable that's an input file to your program? – millimoose Oct 17 '11 at 20:53
  • Both. My main executable would work to do mainBundle, but my I have another executable that won't be running at the time that I need to check versions. – bugfixr Oct 17 '11 at 21:37
  • @rscott: If you can run the other executable, you could add a command line flag that outputs the version and nothing else to stdout. Viewing the executable also tells me that Info.plist is stored in it as cleartext, so you could just open the other executable, look for `CFBundleVersion`, and parse the next XML tag. (Although this is a somewhat fragile solution.) – millimoose Oct 17 '11 at 21:43