How to determine whether code is running in DEBUG / RELEASE build?
Solution 1
Check your project's build settings under 'Apple LLVM - Preprocessing', 'Preprocessor Macros' for debug to ensure that DEBUG
is being set - do this by selecting the project and clicking on the build settings tab. Search for DEBUG
and look to see if indeed DEBUG
is being set.
Pay attention though. You may see DEBUG changed to another variable name such as DEBUG_MODE.
then conditionally code for DEBUG in your source files
#ifdef DEBUG
// Something to log your sensitive data here
#else
//
#endif
Solution 2
For a solution in Swift please refer to this thread on SO.
Basically the solution in Swift would look like this:
#if DEBUG
println("I'm running in DEBUG mode")
#else
println("I'm running in a non-DEBUG mode")
#endif
Additionally you will need to set the DEBUG
symbol in Swift Compiler - Custom Flags
section for the Other Swift Flags
key via a -D DEBUG
entry. See the following screenshot for an example:
Solution 3
Apple already includes a DEBUG
flag in debug builds, so you don't need to define your own.
You might also want to consider just redefining NSLog
to a null operation when not in DEBUG
mode, that way your code will be more portable and you can just use regular NSLog
statements:
//put this in prefix.pch
#ifndef DEBUG
#undef NSLog
#define NSLog(args, ...)
#endif
Solution 4
Most answers said that how to set #ifdef DEBUG and none of them saying how to determinate debug/release build.
My opinion:
Edit scheme -> run -> build configuration :choose debug / release . It can control the simulator and your test iPhone's code status.
Edit scheme -> archive -> build configuration :choose debug / release . It can control the test package app and App Store app 's code status.
Solution 5
Swift and Xcode 10+
#if DEBUG
will pass in ANY development/ad-hoc build, device or simulator. It's only false for App Store and TestFlight builds.
Example:
#if DEBUG
print("Not App Store or TestFlight build")
#else
print("App Store or TestFlight build")
#endif
Related videos on Youtube
P i
Teen-coder (Linux/C++) -> math-grad -> tutor -> freelancer (Mobile specializing in Audio/DSP) -> Software Engineer -> DSP Consultant -> CTO (for cueaudio.com) -> Doing my own thing My most recent placement was as lead engineer for cueaudio.com. However my role quickly elevated to CTO and head of technical staffing. I was able to set the company on the right track by creatively sourcing key talent. The company recovered its $3M seed funding within the first 30 months of operation.
Updated on April 07, 2022Comments
-
P i about 2 years
I am making an app that processes sensitive credit card data.
If my code is running in debug mode I want to log this data to the console and make some file dumps.
However on the final appstore version (ie when it is running in release mode) it is essential all of this is disabled (security hazard)!
I will try to answer my question as best I can; so the question becomes 'Is this solution path the right or best way to do it?'
// add `IS_DEBUG=1` to your debug build preprocessor settings #if( IS_DEBUG ) #define MYLog(args...) NSLog(args) #else #define MYLog(args...) #endif
-
P i about 12 yearsCould you elaborate on exactly what that define is doing? It looks neat, but I don't quite get it. X Usually indicates an Apple reserved macro, whereas PRETTY_FUNCTION indicates something user generated, so the result is confusing
-
Zitao Xiong about 12 yearsxx is format string, you can use whatever you want, if it is identical with the previous string. You can use FUNCTION , but PRETTY_FUNCTION print Objective-C method names. this link explain it very well.
-
Malloc almost 12 yearsThanx for your answer, if i try to make like this:
#ifdef DEBUG NSLog@("Something");#else//#endif
, this doesn't work. How can i initialize a button or log something to the console please, can you edit your question? -
confile over 8 yearsWhere do I find Swift Compiler - Custom Flags?
-
Jeehut over 8 years@confile: I've attached a screenshot that should make clear where to find. Hope it helps!
-
Warpzit over 7 yearsRemember this needs to be defined for the specific framework/extension that use it! So if you have a keyboard/today extension define it there. If you have some other kind of framework same thing. This might only be necessary if the main target is objective c...
-
technophyle over 7 yearsWhat about in Swift?
-
Hiren Prajapati over 6 yearscan I change this macro programatically at run time? I want to enable a button that switches to production APIs. On that button, I want to change DEBUG to 0 and display the message that user needs to restart the app. So next time it will use production APIs.
-
Bhavin_m about 5 yearsAwarded answer!!! it helps me to identify my problem. In my case, I had kept the
Archive
mode toDebug
and submitted the app to the app store. When checking the result after app download from iTunes it simply does not work. So make sure thatDEBUG/RELEASE
only works when selected respective mode inBuild/Run/Archive
. -
ychz about 4 yearsthanks, it seems
Other Swift Flags
key won't appear unless you selectAll
andcombined
above -
bugloaf about 4 yearsThanks! This is what I was missing. I had it set for Clang but not Swift.
-
Murray Sagal over 3 yearsSee kramer's comment on the linked thread. As for kramer, for me this only worked with
-DDEBUG