借鉴这个大神的 传送门 点击打开链接
1.创建一个工程
2.演示用pods 我随便弄了个第三方库执行 pod install 现在新的工程如下图
3.选择PROJECT 的Info 点击+号添加你所需要的debug or release 我已创建好取名为ConfigDebug
4.点开 会看到系统为我们设置好了 我们把它改为None
5.再次执行pod install 完成后如下图
6.新建并配置xcconfig文件 command+n
CommonConfig.xcconfig 文件中放一些通用的配置,例如build version等
其他三个文件分别对应三个build configuration.
在CommonConfig中添加:
1
|
BUILD_VERSION = 1.0.0
|
DebugConfig:
1
2
3
4
5
6
7
8
9
10
|
/*
导入公共 config
*/
#include "CommonConfig.xcconfig"
/*
导入pods 对应的 config
*/
#include "Pods/Target Support Files/Pods/Pods.debug.xcconfig"
APP_DISPLAY_NAME = 测试服
CONFIG_FLAG = DEBUG
|
ReleaseConfig:
1
2
3
4
|
#include "CommonConfig.xcconfig"
#include "Pods/Target Support Files/Pods/Pods.release.xcconfig"
APP_DISPLAY_NAME = 真名
CONFIG_FLAG = RELEASE
|
ConfigDebug:
1
2
3
4
|
#include "CommonConfig.xcconfig"
#include "Pods/Target Support Files/Pods/Pods.releasetest.xcconfig"
APP_DISPLAY_NAME = 正式服
CONFIG_FLAG = RELEASE_TEST
|
以上的 #include "Pods/Target Support Files/Pods/Pods.releasetest.xcconfig" 地址 项目名称的不同导致路径不同
7. 设置Info 把build configuration切换成对应的新建的文件 运行报错 再pod install
取里面的地址 放入 .xcconfig 里面
在Info.plist文件中,设置Bundle name 为
1
|
${APP_DISPLAY_NAME}
|
在scheme中改变build configuration即可实现不同的环境切换,也可以添加多个scheme实现更方便的切换
添加的新scheme需要在manage scheme中勾选shared,git上的其他人才能看到新scheme