利用Xcode的Configuration 配置不同的编译环境
2020-12-23 本文已影响0人
重驹
我们之前项目配置域名都使用这种宏定义的方式,需要什么环境就注释掉另外几个,操作起来比较呆萌,这篇文章说一下如何使用Xcode的Configuration 配置不同的编译环境。

1.配置不同环境下域名
1.1.定义好自己需要哪些环境(不用代码写)
这里创建4种环境:
1.开发人员环境 (Other)
直接连后台人员电脑,与服务器联调使用
2.开发环境(Debug)
完成需求,代码上传,在外网开发服务器调试
3.预发布 (PreRelease)
测试人员使用,copy的正式数据
4.正式环境 (Release)
上传AppStore使用
1.2.定义预编译文件(不用代码写,但是第1.4步要对应上编号)
BUILD_VERSION = 0 -> 开发人员环境 (Other)
BUILD_VERSION = 1 -> 开发环境 (Debug)
BUILD_VERSION = 2 -> 预发布环境 (PreRelease)
BUILD_VERSION = 3 -> 正式环境 (Release)
1.3.在pch中定义不同环境的URL
#if (BUILD_VERSION == 0)
//开发人员环境 (Other)
#define BaseHostUrl @"http://***.*****.**/api/"
#define SERVICEHostURL @"http://***.*****.**"
#elif (BUILD_VERSION == 1)
//开发环境(Debug)
#define BaseHostUrl @"http://***.*****.**/api/"
#define SERVICEHostURL @"http://***.*****.**"
#elif (BUILD_VERSION == 2)
//预发布 (PreRelease)
#define BaseHostUrl @"http://***.*****.**/api/"
#define SERVICEHostURL @"http://***.*****.**"
#elif (BUILD_VERSION == 3)
//正式环境 (Release)
#define BaseHostUrl @"http://***.*****.**/api/"
#define SERVICEHostURL @"http://***.*****.**"
#endif
1.4.配置configuration



切换环境运行,就会看到域名会随着切换的环境进行改变成对应的地址。
备注:如果运行报error: unable to parse contents of file list '/xxx/Pods/Target Support Files
/Pods-xxx/Pods-xxx-frameworks-Debug-input-files.xcfilelist'
类似的错误,是因为pods的库找不到路径了,你改好上面的配置后,再重新pods一下项目,编译应该就可以过了。
2.配置不同环境下的Bundle Id和Display name

分别添加APP_BUNDLE_ID 和APP_DISPLAY_NAME两项,并配置各环境相对应的值

然后点击TARGET->Info,将Bundle identifier 对应的值改成$$(APP_BUNDLE_ID),Bundle name 对应的值改为$(APP_DISSPLAY_NAME)

3.配置不同环境下的AppIcon

然后再点PROJECT->Build Setting 找到Asset Catalog App Icon Set Name,将第一步中添加的icon名称配置到相应环境中。

至此,通过Xcode配置不同环境下的域名、bundle ID、bundle name和appicon就完成了。