Configure iOS app environment variables with xcconfig files

Mango’s Blog

App development usually involves multiple environments, such as Debug, Release, etc.. The environment switch may involve the handover of the server URL, or some third party SDK appid switch. Initially, I set the environment by adding variables in the code:

Static let isRelease = static let = isRelease "prod server URL": "dev server URL true""

However, the disadvantage of this is that each time the environment needs to switch to change the variable, will be more difficult to change after more than. And there may be more than two environments in development. Therefore, the configuration of the app environment variables to better solve the problem of environmental switching.

There are many ways to configure the app environment variables, I chose a way I feel more intuitive configuration – use xcconfig file.

demand

Xcode default will provide two configuration environment: Debug and Release, the difference between the two:

  • Debug will be more debugging information (many people on the Internet said release, the environment can not be broken, and so on, their own test found that what the breakpoint and debug, release does not matter, will be described in detail later
  • Release runs much faster and smoother. The size of the package may be smaller than debug

In my development is mainly used in 3 environments:

  • Test server debug
  • Occasionally switch to the official server debug
  • Shelves (official server, release)

Default is not enough to provide the two…

According to the above requirements, the operation steps are as follows:

1, add Build Configuration

Open the project workspace, enter the main interface of xcode.

Select the main project of project -> info, find Configurations, click on the “+””

Configure iOS app environment variables with xcconfig files
Snip20160813_4.png

You can see the two Configuration: Debug and Release that have been added

Select “deplicate debug configuration”, add a new configuration, named ReleaseTest:

Configure iOS app environment variables with xcconfig files
Snip20160814_5.png

As can be seen from the graph, the three configuration already have the default configuration set (that is, xxconfig file). This is because my project has been included in the cocoapods. The default configuration set is added to the pods.

This time the newly added build configuration does not correspond to the pods of the xcconfig, so the project will be reported wrong. The new configuration corresponding to the set set to none

Configure iOS app environment variables with xcconfig files
Snip20160814_6.png

Command line operation

Pod install

The following figure is completed

Configure iOS app environment variables with xcconfig files
Snip20160814_9.png

2, build and configure the xcconfig file

Common + “n”, select iOS -> Other -> Configuration settings file

Configure iOS app environment variables with xcconfig files
Snip20160814_7.png

New 4 xxconfig file, I use the name:

Configure iOS app environment variables with xcconfig files
Snip20160814_8.png

Which:

CommonConfig.xcconfig file to put some general configuration, such as build version, etc.

The other three files correspond to three build configuration.

Add in CommonConfig:

BUILD_VERSION = 1.0.0

DebugConfig:

/ * config * / public import #include "CommonConfig.xcconfig" pods into the corresponding config / * * / #include "Pods/Target Support Files/Pods/Pods.debug.xcconfig" APP_DISPLAY_NAME CONFIG_FLAG = DEBUG = Test

ReleaseConfig:

#include "CommonConfig.xcconfig" #include "Pods/Target Support Files/Pods/Pods.release.xcconfig" = APP_DISPLAY_NAME = CONFIG_FLAG = RELEASE

ReleaseTestConfig:

#include "CommonConfig.xcconfig" #include "Pods/Target Support Files/Pods/Pods.releasetest.xcconfig" = APP_DISPLAY_NAME = CONFIG_FLAG = RELEASE_TEST

Which:

#include "Pods/Target Support Files/Pods/Pods.releasetest.xcconfig"

The path may be different because of the difference in the name of the project, and if it is not certain it can be pod install. Pods will give hints that contain the correct path.

[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 to Pods/Target Support Files/Pods/Pods.debug.xcconfig or include the Pods/Target Support Files/Pods/Pods.debug.xcconfig in your build configuration.

After the completion of the above, the build configuration switch to the corresponding new file:

Configure iOS app environment variables with xcconfig files
Snip20160814_10.png

2, set the environment variables

Complete the above steps have been added to the environment, the rest is set environment variables

In the Info.plist file, set the Bundle name to

${APP_DISPLAY_NAME}

The name of the application changes according to the configuration.

Configure iOS app environment variables with xcconfig files
Snip20160814_11.png

But what about changing the value of some variables in the code?

1, set the precompiled header parameters

OC

Project -> Build settings -> Apple LLVM 7.1 – Preprocessing

Add in preprocessor

${CONFIG_FLAG}=1
Configure iOS app environment variables with xcconfig files
Snip20160814_12.png

Swift

Project -> Build settings -> Swift Compiler – Custom Flags

Add in other swift flags

D ${CONFIG_FLAG}
Configure iOS app environment variables with xcconfig files
Snip20160814_21.png

Then

In code:

Struct AppConfig enum AppConfigType case Debug {private {case} Release case ReleaseTest private static var currentConfig: AppConfigType {#if DEBUG = 1 return.Debug #elseif RELEASE_TEST return.ReleaseTest #else return = 1.Release #endif static var webServerURL: String {switch} currentConfig {case.Debug: "return test URL" default: return "release URL"}}}

Other variables can also be configured in the above manner.

Add multiple scheme for easy configuration switching

Configure iOS app environment variables with xcconfig files
Snip20160814_14.png
Configure iOS app environment variables with xcconfig files
Snip20160814_18.png

Change in scheme build configuration can be achieved in different environments, you can also add multiple scheme to achieve a more convenient switch

Configure iOS app environment variables with xcconfig files
Snip20160814_19.png
Configure iOS app environment variables with xcconfig files
Snip20160814_20.png

Added new scheme needs to be selected in the manage shared scheme, GIT on the other people can see the new scheme

Configure iOS app environment variables with xcconfig files
Snip20160814_22.png

DEMO and recommendations

XCConfig Demo

Hand taught you to configure a iOS app with multiple environment variables