我正在做一个处理敏感信用卡数据的应用程序。

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

然而,在最终的appstore版本(即当它运行在发布模式时),必须禁用所有这些(安全隐患)!

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

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

#if( IS_DEBUG )  
#define MYLog(args...) NSLog(args)  
#else  
#define MYLog(args...)  
#endif  

当前回答

还有一个想法需要检测:

调试模式.h

#import <Foundation/Foundation.h>

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

调试模式.m

#import "DebugMode.h"

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

添加到头桥文件:

# include“DebugMode.h”

用法:

DebugMode isDebug()。

不需要在项目属性swift标志中写入内容。

其他回答

不确定我是否回答了你的问题,也许你可以试试这些代码:

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

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

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

大多数回答说如何设置#ifdef DEBUG,没有人说如何确定调试/发布构建。

我的意见:

编辑方案->运行->构建配置:选择调试/发布。它可以控制模拟器和测试iPhone的代码状态。 编辑方案->存档->构建配置:选择调试/发布。它可以控制测试包应用程序和app Store应用程序的代码状态。

还有一个想法需要检测:

调试模式.h

#import <Foundation/Foundation.h>

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

调试模式.m

#import "DebugMode.h"

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

添加到头桥文件:

# include“DebugMode.h”

用法:

DebugMode isDebug()。

不需要在项目属性swift标志中写入内容。

Swift和Xcode 10+

#如果DEBUG将传入任何开发/特别构建、设备或模拟器。这只适用于App Store和TestFlight版本。

例子:

#if DEBUG
   print("Not App Store or TestFlight build")
#else
   print("App Store or TestFlight build")
#endif