In normal development, it is common to encounter multiple environments for development, such as: Published to online when using the online environment, and development is a development environment, test environment is a test environment, in this case, we need to come back and modify the project in the interface address, file upload, file preview requested address and so on, very troublesome, and if there is a confused, the project is easy to appear the bug, So today we’ll explore how to configure multi-environment development.

For now, explore the following three:

1. Configure multiple targets

2. Configure it in Build Settings

3. Configure from the xcConfig file (highly recommended)

The advantages and disadvantages of the three methods are analyzed by explaining their configuration methods in detail

Configure multiple Target

1. Create multiple TARGETS, select Target and then select TARGETS, as shown below:

2. Select the Target of the current project and click Copy, then modify the Target name and info.plist after Copy, as shown below:

3. Modify the Display Name and Bundle Indentifier of the Target, and modify the info.plist file path in the Build Settings of the Target, as shown in the following figure:

4, Then select a different Target to run the project, we will find that it is different APP, this is more suitable for the same source, but need to customize different names and IDS of the APP.

5. If you need to determine the Target in your code, you need to set it in the Build Settings:

OC project: Search for Macors and add your own preprocessor as shown below:

Swift project: search swift_flag and add your own preprocessing command. Remember to add -d before setting preprocessing command, as shown below:

Advantages:

1. You can create multiple targets, and a single modification will not affect the channel. You can customize APP Icon, name, ID, etc

2. Different code logic can be added or subtracted through different preprocessing instructions

Disadvantages:

1. The configuration is troublesome and needs to be modified in many places

2. The configuration process can be confusing

3, when the multiplayer development, the occurrence of bugs increased the difficulty of investigation

configurationBuild Settings

1. Select Target in the project, then Select Build Settings, then click the top plus sign, select Add User-defined Setting, then set the custom field name, such as BASE_URL, and then set the value for different modes, as shown below:

2. Select Info.plist and add a custom field. The field key can be customized, but the value must follow the pattern shown in the following figure, and the field must be the user-defined field key added in the Build Settings, as shown below:

3. Then use it in code, in DEBUG mode, as shown below:

4. The system will obtain the values we set in different modes according to our different modes, so as to achieve the effect of configuring different domain names in different environments. Other values can also be set

Advantages:

1. Relatively few configurations

2, through the custom field to achieve the effect of different environment configuration of different address

Disadvantages:

1. Very limited and less readable than multiplayer development

configurationxcconfigfile

Speaking of xcconfig files, I think many people are both familiar and unfamiliar. Why? First of all, we’re developing as iOS, and basically everyone has worked with Cocoapods, and when Cocoapods manages a tripartite library, the configuration file they use is an.xcconfig file, so it’s relatively familiar, because you’ve seen it, almost every project has it, but it’s strange because it’s common, But did not go to understand what is used to do, what is the role, today through the configuration of multiple environment together to see

1. First we create a config folder in the project, then Commond + N, enter Configuration Settings File in the search box, generally as long as enter CON will appear, then select the File, click Continue, set the name, and click Continue, as shown below:

2. Then we carry out some relevant configuration in the corresponding config file, such as loading some dependent libraries or domain names in multi-environment, etc

First: add a custom field to info.plist, as shown below:

Next: configure in the.xcconfig file, as shown below:

Finally: use it in code, as shown below:

Advantages:

1. You can set environment variables for different schemes

2. We can add dependent libraries and other tools for different schemes

3. Easy to manage and configure

Disadvantages:

no

So that’s the configuration for multi-environment development, some personal exploration experiences and opinions.