然而这样做的缺陷是,每一次要切换环境的时候都需要去更改这个变量,多次修改后还是会比较烦。并且通常开发中可能会有不止两个环境。所以,配置app环境变量能够更好的解决环境切换的问题。
配置app环境变量的方式有多种,我选择了一种我感觉比较直观的方式来配置——使用xcconfig文件。
Xcode默认会提供两种配置环境:Debug 和 Release,这两者的区别:
Debug 会多一些调试信息(网上很多人说release,环境下不能断点之类的,自己测试后发现断点什么的和debug、release没有关系,后面会详细说)Release 运行速度快很多,流畅。打的包大小可能要比debug小一些在我的开发中主要是会用到3种环境:
测试服务器debug偶尔会切换到正式服务器debug上架(正式服务器,release)默认提供的两种还不太够用。。。
根据以上需求,操作步骤大致如下:
打开项目的workspace,进入xcode的主界面。
选中主要工程的project -> info , 找到Configurations, 点击下方的“+”
Snip20160813_4.png从图中可以看到两个已经添加好的Configuration: Debug 和 Release
选择“deplicate debug configuration”,添加一个新的configuration,命名为ReleaseTest:
Snip20160814_5.png从图上可以看到,三个configuration都已经有了默认的configuration set(也就是xxconfig文件)。这是因为我的工程中已经包含了cocoapods。默认的configuration set是pods添加的。
这个时候新增加的build configuration并没有对应的pods的xcconfig,所以项目会报错。把新建的configuration 对应的set 设置为none
Snip20160814_6.png命令行运行
pod install完成之后如下图
Snip20160814_9.pngcommon + "n", 选择 iOS -> Other -> Configuration settings file
Snip20160814_7.png新建4个xxconfig文件,我采用一下命名:
Snip20160814_8.png其中:
CommonConfig.xcconfig 文件中放一些通用的配置,例如build version等
其他三个文件分别对应三个build configuration.
在CommonConfig中添加:
BUILD_VERSION = 1.0.0DebugConfig:
/* 导入公共 config */ #include "CommonConfig.xcconfig" /* 导入pods 对应的 config */ #include "Pods/Target Support Files/Pods/Pods.debug.xcconfig" APP_DISPLAY_NAME = 测试服 CONFIG_FLAG = DEBUGReleaseConfig:
#include "CommonConfig.xcconfig" #include "Pods/Target Support Files/Pods/Pods.release.xcconfig" APP_DISPLAY_NAME = 真名 CONFIG_FLAG = RELEASEReleaseTestConfig:
#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"可能会因为项目名称的不同导致路径不同,如果不太确定的可以再次pod install。pods会给出提示,其中包含了正确的路径。
重点: pod 使用说明
[!] CocoaPods did not set the base configuration of your project because your project already has a custom config set. In order for CocoaPods integration to work at all, please either set the base configurations of the target configTest toPods/Target Support Files/Pods/Pods.debug.xcconfig or include the Pods/Target Support Files/Pods/Pods.debug.xcconfig in your build configuration.
完成以上后,把build configuration切换成对应的新建的文件:
Snip20160814_10.png完成上面的步骤就已经添加好了环境,剩下的就是设置环境变量
在Info.plist文件中,设置Bundle name 为
${APP_DISPLAY_NAME}应用的名称就会根据配置改变了。
Snip20160814_11.png然而在代码中需要根据环境改变某些变量的值怎么办呢?
Project -> Build settings -> Apple LLVM 7.1 - Preprocessing
在 preprocessor 中添加
${CONFIG_FLAG}=1 Snip20160814_12.pngProject -> Build settings -> Swift Compiler - Custom Flags
在 other swift flags 中添加
-D ${CONFIG_FLAG} Snip20160814_21.png代码中:
struct AppConfig { private enum AppConfigType { case Debug case Release case ReleaseTest } private static var currentConfig: AppConfigType { #if DEBUG = 1 return .Debug #elseif RELEASE_TEST = 1 return .ReleaseTest #else return .Release #endif } static var webServerURL: String { switch currentConfig { case .Debug: return "test url" default: return "release url" } } }其他变量也可以采用以上方式配置。
在scheme中改变build configuration即可实现不同的环境切换,也可以添加多个scheme实现更方便的切换
Snip20160814_19.png Snip20160814_20.png添加的新scheme需要在manage scheme中勾选shared,git上的其他人才能看到新scheme
Snip20160814_22.pngXCConfig Demo
http://www.jianshu.com/p/9b8bc8351223