Xcode/iOS:如何确定代码是否在DEBUG/RELEASE构建中运行?

P i*_*P i 227 debugging xcode preprocessor release ios

我正在制作处理敏感信用卡数据的应用程序.

如果我的代码在调试模式下运行,我想将此数据记录到控制台并进行一些文件转储.

但是在最终的appstore版本上(即它在发布模式下运行时)必须禁用所有这些(安全隐患)!

我会尽力回答我的问题; 所以问题就变成了"这个解决方案路径是正确的还是最好的方式?"

// add `IS_DEBUG=1` to your debug build preprocessor settings  

#if( IS_DEBUG )  
#define MYLog(args...) NSLog(args)  
#else  
#define MYLog(args...)  
#endif  
Run Code Online (Sandbox Code Playgroud)

Dam*_*amo 242

在"Apple LVM - 预处理","预处理器宏"下检查项目的构建设置以进行调试,以确保设置"DEBUG" - 通过选择项目并单击构建设置选项卡来执行此操作.搜索'DEBUG'并查看是否确实设置了DEBUG.

但请注意.您可能会看到DEBUG已更改为另一个变量名称,例如DEBUG_MODE.

我的项目设置的Build Settings选项卡

然后在源文件中有条件地为DEBUG编码

#ifdef DEBUG

// Something to log your sensitive data here

#else

// 

#endif
Run Code Online (Sandbox Code Playgroud)

  • 它应该工作.为什么不用一些代码发布新问题? (2认同)

Jee*_*hut 117

有关Swift的解决方案,请参阅SO 上的此主题.

基本上Swift中解决方案看起来像这样:

#if DEBUG
    println("I'm running in DEBUG mode")
#else
    println("I'm running in a non-DEBUG mode")
#endif
Run Code Online (Sandbox Code Playgroud)

此外,您还需要通过条目在键的部分设置DEBUG符号.请参阅以下屏幕截图以获取示例:Swift Compiler - Custom FlagsOther Swift Flags-D DEBUG

在此输入图像描述

  • @confile:我附上了一个截图,应该清楚找到的位置.希望能帮助到你! (2认同)
  • 从 Xcode 9+ 开始,您[不再需要更改新 Swift 项目的项目设置](/sf/answers/5245354201/)。 (2认同)

Nic*_*ood 88

Apple已DEBUG在调试版本中包含一个标志,因此您无需定义自己的标志.

您可能还想考虑NSLog在不处于DEBUG模式时重新定义为空操作,这样您的代码将更加可移植,您可以只使用常规NSLog语句:

//put this in prefix.pch

#ifndef DEBUG
#undef NSLog
#define NSLog(args, ...)
#endif
Run Code Online (Sandbox Code Playgroud)


小智 30

大多数答案说如何设置#ifdef DEBUG并且没有人说如何确定调试/发布版本.

我的看法:

  1. 编辑方案 - >运行 - >构建配置:选择调试/发布.它可以控制模拟器和测试iPhone的代码状态.

  2. 编辑方案 - >存档 - >构建配置:选择调试/发布.它可以控制测试包应用程序和App Store应用程序的代码状态. 在此输入图像描述


Kir*_*aev 20

SwiftXcode 10+

#if DEBUG将通过任何开发/临时构建、设备或模拟器。对于 App Store 和 TestFlight 构建,它只是错误的。

例子:

#if DEBUG
   print("Not App Store build")
#else
   print("App Store build")
#endif
Run Code Online (Sandbox Code Playgroud)


vik*_*mar 15

为使用Kotlin 多平台ios 调试模式的人员添加此功能。以下是如何确定构建是调试还是发布的。

if (Platform.isDebugBinary) {
     NSLog(message ?: "", "")
}
Run Code Online (Sandbox Code Playgroud)


Sen*_*ful 9

Swift + Xcode 14 更新...

从 Xcode 8 开始,您应该使用“Active Compilation Conditions”而不是“Other Swift Flags”从Xcode 9.3开始,Apple 已经包含了DEBUG新项目的值:

在此输入图像描述

这意味着您不需要更改任何项目设置,并且可以立即使用#if DEBUG

#if DEBUG
  print("something")
#endif
Run Code Online (Sandbox Code Playgroud)


geo*_*war 8

zitao xiong的回答非常接近我的用法; 我还包括文件名(通过剥离FILE的路径).

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


Fa.*_*uri 7

在Xcode 7,有下田间苹果LLVM 7.0 -预处理,这被称为" 预处理器宏未使用预编译...... "我把DEBUG前面调试,它通过使用下面的代码为我工作:

#ifdef DEBUG
    NSString* const kURL = @"http://debug.com";
#else
    NSString* const kURL = @"http://release.com";
#endif
Run Code Online (Sandbox Code Playgroud)


Vya*_*lav 6

还有一个想法要检测:

调试模式.h

#import <Foundation/Foundation.h>

@interface DebugMode: NSObject
    +(BOOL) isDebug;
@end
Run Code Online (Sandbox Code Playgroud)

调试模式.m

#import "DebugMode.h"

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

添加到头桥文件:

#include "DebugMode.h"

用法:

DebugMode.isDebug()

不需要在项目属性 swift 标志中写一些东西。