Can an iOS app programmatically detect if it's built for release or debug?

Is it possible for an iOS app to programmatically detect if its built for TestFlight/App Store distribution versus built for development?

The motivation for doing this is so that the app can detect if a push server should send pushes using the Apple production server or the sandbox server - when the app sends the push token to the server, I'd like it to additionally send an indicator to the server so the server knows which of the Apple servers to use.

Is there a way to achieve this?

TIA

This is actually quite easy.

#if DEBUG
    let pushEnvironment = "development"
#else
    let pushEnvironment = "production"
#endif

This would work in most circumstances and with common Xcode project setups, as by default Xcode sets the aps-environment entitlement to development or production based on whether it is built for debugging or release.

Unfortunately this is not foolproof, as there could be scripts that change the entitlement with a build script, and so on. If you know your builds are simple, then this would work. But it is definitely not a foolproof approach.

The entitlements are built into your signed app and don't exist in a recognizable form, so it is not possible to read them while running. You could possibly use some creative build script engineering to read the aps-environment entry from the file, but with so many variables that goes into anything but the simplest build configurations, this would also be fragile.

So, if this is for your own project (as in, you're not building a library for general use that will answer this question), and you know no build shenanigans are going on the #if DEBUG construct might be the easiest solution.

@Engineer

The problem with this approach is that it assumes a debug build of code equates to the development push server and a release build equates to the production server, which is not the case.

Its actually not the build, as I said in my question that is important and dictates the push server, its the distribution signing that is.

You might have a debug build of code that you want to publish to TestFlight; or you might have a body of code that you first test running with Xcode,then without modifying it you want to build and publish to testflight/App Store; or you might want to create an archive, and then distribute that same archive to TestFlight or as a developer distribution .ipa, and so on.

Maybe I could look through the Firebase Crashlytics code, they detect when code is debuggable i.e. when the get-task-allow entitlement is set, I expect that would be useful and relevant to obtain in this case as that will vary with the distribution signing.

Can an iOS app programmatically detect if it's built for release or debug?
 
 
Q