1.在 PROJECT 中的 Info的 configurations 下面基于 Release 创建 Test 的 configuration

image-20250315110251790

2.在preprocessor Macros 下面的 Test 中增加 TEST=1

image-20250315110418462

3.在Build Settings 下的custom flags 下面的 Test 下增加 TEST

image-20250315110522558

4.点击 Manage Schemes, 增加-Test 和-Release, 并修改他们的编译条件

image-20250315110649454

image-20250315110809948

5.添加Configurations各个环境下的文件,并分别配置 APP_AAME, bundleID 等变量,并关联到Configurations下的配置中

image-20250315110921655

image-20250315111102837

6.在 Info.plist 文件中将变量和配置文件中的定义关联

1
2
3
4
5
6
7
8
9
10
<key>CFBundleDisplayName</key>
<string>$(APP_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>UIFileSharingEnabled</key>
<string>$(ENABLE_FILE_SHARING)</string>
<key>Configuration</key>
<string>$(CONFIGURATION)</string>

image-20250315111222685

7.打开项目的project.pbxproj文件,将PRODUCT_BUNDLE_IDENTIFIER后面的值改为”$(PRODUCT_BUNDLE_IDENTIFIER)”

image-20250315111442409

image-20250315111631287

8.添加测试代码,运行各个环境,看是否配置正确

1
2
3
4
5
6
7
8
9
10
        Configuration.log()

#if DEBUG
let BASE_URL = "www.xsense.dev"
#elseif TEST
let BASE_URL = "www.xsense.test"
#else
let BASE_URL = "www.xsense.release"
#endif
print("BASE_URL:\(BASE_URL)")

代码地址:

https://gitee.com/chengft/ios_demos/SwiftMultiEnvironment