Getting access to SharedSupport sub-folder of bundle

So if my executable is in the MacOS directory, am I correct in thinking the such things as a related command line application, the help files and other stuff should go into "Shared Support"?

If yes, how can my C++ application find and use "stuff" in that SharedSupport sub-directory of the bundle (e.g. open a file or display the help files)?

Thanks David

Answered by DTS Engineer in 837409022

Weird. I’ve discussed Contents/SharedSupport exactly four times in the last 17 years here on DevForums, and two of them have been this week (-:

Anyway, my advice is that you not use SharedSupport. See here. Rather, follow the advice in Placing Content in a Bundle.

Share and Enjoy

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

Weird. I’ve discussed Contents/SharedSupport exactly four times in the last 17 years here on DevForums, and two of them have been this week (-:

Anyway, my advice is that you not use SharedSupport. See here. Rather, follow the advice in Placing Content in a Bundle.

Share and Enjoy

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

Regardless of whether you are using SharedSupport or some other resources folder, there are a number of different ways to access them.

Your first problem is going to be language. It's a Swift world now. If you're writing a Swift app, then all these APIs are easier to use, at the cost of a bunch of new Swift-isms. But I'm guess that's a non-starter in this case, so I'll move on.

Swift or not, the Mac development and Xcode is not C++ friendly. There are a couple of different ways to approach the problem. You can use the old C APIs. These are referred to as "Core Foundation" and virtually always have a "CF" prefix. This kind of code is clunky and hard to use. I really only mention it in case you find it on your own so you know to avoid it.

A much better idea is to release your grip on C++ just a little bit and use Objective-C. You might even consider Objective-C++. The trick here is to make sure that your pure C++ code never sees any Objective-C code. While Objective-C++ can speak both, plain C++ cannot. So you can write some public headers that define some C++-friendly functions you can call. Then, you pair that public header with a private Objective-C header/source set to actually get access to these folders. It sounds more complicated than it is. This is the easiest way - trust me. ARC alone is worth it. And getting your strings into C++ will be much easier.

At that point, you can access any of those bundle paths through the "NSBundle" class. This class is stable, well-documented and relatively easy to use. Most such code is "toll-free bridged" with a Core Foundation counterpart. So compare the NSBundle methods with their "CFBundle...." versions. As I mentioned before, you're probably looking for path strings. That will be much easier with NSStrings than CFStrings.

There is also a different set of functions for the various accessing application support folders that might exist in a "Library" folder. These belong to the "NSFileManager" class. Look for the "URLsForDirectory" methods. I assume there is a CoreFoundation version of these, but I don't know about them. These methods are a little trickier to use. You have to specify domains and results are arrays. Plus, you're expected to create folders if they don't already exist. It's a lot of boilerplate and error handling.

While Objective-C++ is an awesome tool, it’s not your only option these days. Swift’s C++ interop is pretty cool, and it’s gets cooler with each release.

Which one you use depends on the abstraction you’re trying to build:

  • If you want to write your glue on the C++ side, Objective-C++ is a great choice.

  • However, there are cases where it’s better to do put the glue on the Swift side. The most obvious example of that is when interacting with a Swift-only API. That’s not the case here, but it’s getting increasingly common. In that case the choice of Swift to C++ is much nicer than Swift to Objective-C to Objective-C++ to C++.

Share and Enjoy

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

Getting access to SharedSupport sub-folder of bundle
 
 
Q