Multi-environment configuration

  • Project: Contains all the code, resource files, and information for the Project.
  • Target: Specifies how the code and resource files are built.
  • Scheme: specifies the environment configuration for the Target.
throughpreprocesor macroTo configure multiple environments

Make decisions based on macros in the code to achieve multi-environment configuration

throughuser_defined+Info.plist+ a singleTargetTo configure multiple environments

chooseDebugThe environmentSame way to chooseReleaseEnvironment is running

throughuser_defined+Info.plistMore than +TargetTo configure multiple environments

New constructionTargetDemoIs created in the projecttargetnamedTargetDemo2

Change the plist file name to targetDemo2-info.plist

You can see that different targets can set their own App Name, Bundle ID, Icon, and so on

If the two targets are set to the same Bundle Id then they can be considered different environments of the same App, such as Dev and Release

If the two targets have different Bundle ids, then they are completely different apps

Here we demonstrate the useTargetDemoconfigurationDebugEnvironment, useTargetDemo1configurationReleaseEnvironment, in the creationTargetThe system also created the correspondingScheme

The selectedTargetDemochooseDebugmodelThe selectedTargetDemo1chooseReleasemodel

Because we’ve already configured itHOST_URL

Run respectively

When creating a Target, the system creates the corresponding Scheme for us. Therefore, we can not create multiple targets, but create multiple schemes to implement multiple environment configuration

throughuser_defined+Info.plistMore than +SchemeTo configure multiple environments

chooseEdit SchemeTo configure

Again, I have to configure itHOST_URL

The first configurationTargetDemoforDebugThe environment

Configure TargetDemo1 as the Release environment

Select respectivelyTargetDemoandTargetDemo1run

throughuser_defined+Info.plist+XconfigTo configure multiple environments

You can deleteTargetConfiguration of theuser_defined

Create a folder namedxconfig, respectivelyDebugandReleaseThe configuration file of the schema is named asFolder name -Target name. Model nameI’m going to have folders called,xconfig-TargetDemo.debug.xconfigandxconfig-TargetDemo.release.xconfig, respectively

Enter the content separately

Choose differentSchemerun

Go to theTargetDemoThe correspondingBuilding SettingsCheck, finduser_definedAutomatically generatedHOST_URLThese are just two different ways of configuring files and visualizing them

Summary of configuration modes in multiple environments

  • Use precompiled macros to make environment judgments in your code
  • throughUser-Defined+Info.plistWay to configure
    1. Using a singleTargetAnd a singleScheme, which requires each run to modifySchemeThe environment
    2. The use of multipleScheme, eachSchemeIt corresponds to an environment
  • xconfigIs to configure theUser-DefinedIn another way
  • moreTargetIt’s more like multiple apps, and this way of configuring multiple environments is overkill

Configuration conflicts

Create a new project and execute the commands separately in the root directorypod initandpod installOpen the project to see that two configuration files are automatically generated

This is already configured by default

But if we create our ownxconfigConfiguration files and Settings required#includeThe introduction ofPodCreated configuration file

You need to either set base Configurations to Pods- targetDemo.debug.xconfig or include it in config-targetDemo.debug.xconfig

inpodsAdd to fileAFNetworkingAnd performpod installafter

We also configure it in our own configuration fileOTHER_LDFLAGS = -framework "SDWebImage"

You need to use keywords here$(inherited)Inherits the previous configuration

We can also be inBuilding SettingsTo modify the configuration

The priorities are in descending order

  • Manually modifyBuilding Settings
  • Modify theBasic, which is its own configuration file
  • Modify thePodThat is, the inherited file

Reference documentation

xcodebuildsettings