简体   繁体   中英

How to determine whether code is running in DEBUG / RELEASE build?

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  

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

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:

在此处输入图片说明

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

Most answers said that how to set #ifdef DEBUG and none of them saying how to determinate debug/release build.

My opinion:

  1. Edit scheme -> run -> build configuration :choose debug / release . It can control the simulator and your test iPhone's code status.

  2. Edit scheme -> archive -> build configuration :choose debug / release . It can control the test package app and App Store app 's code status. 在此处输入图片说明

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 build")
#else
   print("App Store build")
#endif

zitao xiong's answer is pretty close to what I use; I also include the file name (by stripping off the path of FILE ).

#ifdef DEBUG
    #define NSLogDebug(format, ...) \
    NSLog(@"<%s:%d> %s, " format, \
    strrchr("/" __FILE__, '/') + 1, __LINE__, __PRETTY_FUNCTION__, ## __VA_ARGS__)
#else
    #define NSLogDebug(format, ...)
#endif

In xcode 7, there is a field under Apple LLVM 7.0 - preprocessing , which called " Preprocessors Macros Not Used In Precompiled... "? I put DEBUG in front of Debug and it works for me by using below code:

#ifdef DEBUG
    NSString* const kURL = @"http://debug.com";
#else
    NSString* const kURL = @"http://release.com";
#endif

Just one more idea to detect:

DebugMode.h

#import <Foundation/Foundation.h>

@interface DebugMode: NSObject
    +(BOOL) isDebug;
@end

DebugMode.m

#import "DebugMode.h"

@implementation DebugMode
+(BOOL) isDebug {
#ifdef DEBUG
    return true;
#else
    return false;
#endif
}
@end

add into header bridge file:

#include "DebugMode.h"

usage:

DebugMode.isDebug()

It is not needed to write something inside project properties swift flags.

Adding this for people working with Kotlin multiplatform ios debug mode. Here is how you can determine if the build is debug or release.

if (Platform.isDebugBinary) {
     NSLog(message ?: "", "")
}

Not sure if I answered you question, maybe you could try these code:

#ifdef DEBUG
#define DLOG(xx, ...)  NSLog( \
    @"%s(%d): " \
    xx, __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__ \  
    )
#else
#define DLOG(xx, ...)  ((void)0)
#endif 

So I found my way here while trying to figure out why my code wasn't executing in my release build, and none of the other answers address how to make #if RELEASE work.

I didn't want anything to happen in DEBUG mode, so I just used #if RELEASE, assuming (incorrectly) that it would just work.

#if RELEASE // do something release specific here #endif

You can (as of XCode 14 at least) go to the Project -> Combined -> Swift Compiler - Custom Flags (see screenshot below). Add a RELEASE value to the Release section, and #if RELEASE will work as above.

Note that you don't want to hit the + next to the blank space by release, just double-click the blank space. If you do, it'll add an "Any Architecture / Any SDK" Line, which makes the RELEASE flag not work properly.

Correct version: 在此处输入图像描述

Incorrect version: 在此处输入图像描述

Swift + Xcode 14 update...

As of Xcode 8, you should be using "Active Compilation Conditions" instead of "Other Swift Flags" . As of Xcode 9.3 , Apple already includes the DEBUG value for new projects:

在此处输入图像描述

This means you don't need to change any project settings , and can simply use #if DEBUG immediately:

#if DEBUG
  print("something")
#endif

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