简体   繁体   中英

Detect if the iPhone is running a Debug/Distribution build at runtime

是否可以在运行时检测正在运行的应用程序是使用调试还是分发编译的。

In the Project Info, for a Debug configuration, add a Preprocessor Macro of "DEBUG" (in the GCC 4.2 - Preprocessing section).

In your code you can use #ifdef to see if DEBUG is defined if you want some code included or not for debug builds. Or you can even set a variable (I can't imagine why you would want this):

#ifdef DEBUG
  BOOL isBuiltDebug = YES;
#else
  BOOL isBuiltDebug = NO;
#endif

EDIT: Well, another way is to define a boolean value in a Preprocessor Macro, ie: "DEBUG_BUILD=1" for the Debug configuration, and "DEBUG_BUILD=0" for the Release configuration. Then you can use that value in your code:

if (DEBUG_BUILD) {
   ....
}

Just be careful not to use a macro name that could match a name that is already in your code or in any .h file that you might include either, because the preprocessor will replace it and it's a real pain to find those kinds of bugs.

Without having to think about defining a custom preprocessor macro, you can just write a custom method like this one :

+ (BOOL) isInDebugMode
{
    #ifndef __OPTIMIZE__   // Debug Mode
        return YES;
    #else
        return NO;
    #endif
}

Or just write your code inline within those statements :

    #ifndef __OPTIMIZE__   // Debug Mode
       // Your debug mode code
    #else
        // Your release mode code
    #endif

The __OPTIMIZE__ preprocessor setting in automatically set by the compiler regarding your project settings, so you don't have to worry about it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM