Search code examples
macossandbox

How can an application tell silently if it's running in the sandbox?


I am writing an app that can run sandboxed or not.

I like to keep my code independent on whether I later codesign it for sandboxing or not, i.e. I want to have having a constant in my code (or in the build environment) that I have to change for my code to know whether it'll be built for a sandbox or not.

Naturally, I need to use different APIs in some cases in my code depending on whether the app is sandboxed or not.

So, I like to have code in my app that dynamically detects whether it's running in the sandbox or not. And I like to do this without getting a message in the console log. I.e, trying to access a file that's inaccessible in the sandbox is not a good solution because that'll cause a log entry which in turn would irritate users of my app, thinking there's something wrong.


Solution

  • I found a much simpler trick now:

    I get the path to the Preferences folder. If it looks something like this, I'm sandboxed:

    /Users/<user>/Library/Containers/<bundle_id>/Data/Library/Preferences
    

    That's enough for my needs, I just want to avoid seeing console msgs. Should Apple ever change the path and my test fails, then the worst I'll see is a console msg about a denied operation. I can live with that.