Multi-environment configuration

Let’s start with three concepts

  1. Project: contains all the code, resource files, and all the information of the project
  2. Target: Specifies how the code and resource files are built
  3. Scheme: Configures the environment for the specified target

In our actual development process, we may need to generate multiple targets for a project in different configurations, so the next step is how to configure multiple environments

Three ways to configure multiple environments

Multiple targets

The specific process is as follows:When wecopyatargetAnd then, thiscopythetargetWill stay with the quiltcopythetargetSame configuration. ChangeLoginApp copyforLoginApp-DevIn order to maintain consistency, we need to make corrections in the following areas of the projectIn order to distinguish between the two sets of environments, we need to define macros up front, as you can see in theDebugEnvironment, has been defined in advanceDEBUGWe can also add custom macros to this macroThe following isswiftHow to add macrosIn the code, you can add new macros to do the control.

The above method is generally cumbersome, generating multiple PList files, and more configuration

The scheme configuration

In this way we add aconfigurationWhen we edit scheme, we will find that we have one moreconfiguration This can be changed during runconfigurationTo configure the environment, or by creating a newscheme And then we’re going to take this newly createdschemetheconfigurationSet toDEV After going through the above two steps, we adduser-defined value HOST_URLAnd stored inplistIn the following code can be passedload plistFile, obtained fromHOST_URLThe value of the

In this way, it is easy to configure multiple environments without generating new targets. This method is relatively simple compared to the first method, but I will still configure some environment variables through build setting

Xconfig file configuration

We can go throughxconfigFile to configure the environmentCreate a new folder,new file -> configuration fileThe naming rule starts with the name of the current folder, followed by the name of the app, and then the environment, as shown in the following figurethisxconfigThe file is just storing somekey value, we created twoxconfigFile will then be selected for the correspondingconfigurationUnder theconfig fileWhere the top is rightprojectThe following configuration is correcttargetThe configuration of theYou will then enter something in the Config fileAlso configure the plist fileAnd eventually we can get it out in codeplistIn theHOST_URLThe value of PI is equal to PIxconfigI’m going to set a value in

For example, OTHER_LDFLAGS is an abbreviation of other Link flags in build setting. For details, see the website