“I walk slowly, but I never walk backward.”

I am a slow walker, but I never walk backwards

Main Contents:

  1. Distinguish betweenProject,Target,Scheme
  2. moreTargetconfiguration
  3. Schemeconfiguration
  4. xcconfigConfiguration file

1. Distinguish Project, Target, Scheme

  • Project: is a project as a whole, equivalent to a repository, including all the code and resource files;
  • Target: is equivalent to a concrete product, including the code, resource file specific usage rules and configuration;
  • Scheme: the specifiedTargetTo configure the environment;

Conclusion: A Project can contain multiple targets, that is to say, different apps can be generated by different targets;

There are three ways to configure multiple environments:

  1. moretargetConfiguration;
  2. SchemeConfiguration;
  3. xcconfigFile configuration;

2. Multi-target configuration

1. Summarize the characteristics
  • Advantages: Easy to manage code, resource files, such as inCompile Sources,Copy Bundle ResourcesTo add or remove code and resources to compile as needed;
  • Disadvantages: The configuration is cumbersome and involves multiple configurationsinfo.plistFile, macro definition, etc., a large number of people easy to modify confusion;
2. Basic steps

1. Create a target

Selected fortarget, right-click and selectDuplicate, will copy created with the originalTargetNew with the same configurationTarget;

2. Modify the configuration of the new target

Since the copied target still uses the old configuration, we need some additional Settings to use the new target:

  1. Modify the newTargetThe name;
  2. Modify the newinfo.plistThe name;
  3. Modify theBuild Settings -> Packaging -> info.plist FileProduct Bundle identifier;

3. Change the Scheme name

Enter theScheme Manager, modifySchemeThe name of the, here is not necessary, modified to make it easier to identify;

4. Add predefined macros to distinguish different targets

configurationOCEngineering:Build Settings -> Preprocessor Macros

configurationSwiftEngineering:Build Settings -> Other Swift Flags

Understand -d

  1. swiftcIn the compiler, you need to add one-Dtheoption, represents marking the conditional compilation flag astrue;
  2. As for the-DCan also be viewed using the console command:swiftc --help |grep '-D'
 -D <value>  Marks a conditional compilation flag as true
Copy the code

5. Test target multiple environments

#if DEV
    print("Project DEV")
#else
    print("Project Normal")
#endif
Copy the code

3. Scheme Configuration

Scheme configurations implement multiple environments by setting multiple configurations. The steps are as follows:

1. Add a new configration

Go to Project -> Info -> Configurations and you can see the Debug, Release, and other Configurations that already exist. Click “+” to add a new Configuration (for example, Dev);

2. Switch to multiple environments

increaseconfigurationLater, inEdit SchemeThere will also be one moreconfigration. We are inrun“, you can manually switchconfigrationUse different environments;

3. The Scheme

Manual switching each timeconfigrationIt’s a little bit more tedious, but we can use different onesSchemeDirectly corresponding to different configurations;

4. Test Scheme multi-environment

1.Build Setting -> User-DefinedIn the customHost_URLAnd then to differentconfigrationConfigure different values;

2.Host_URLConfigure as a variableTo the info. The plistFile;

3. Read info.plist and manually switch to ration or run it in a different Scheme.

guard let path = Bundle.main.path(forResource: "Info", ofType: "plist") else { return}
guard let infoDic = NSDictionary(contentsOfFile: path) else {return}
if let host_url = infoDic["HOST_URL"] {
    print("HOST_URL:\(host_url)")}Copy the code

The XCConfig configuration file

Xcconfig configuration file:

  1. .xcconfigConfiguration files, which help us to configure in the form of filesbuild settingsThe contents of;
  2. .xcconfigThe content of the document iskey-value2. Formed of form;
  3. increase.xcconfigAfter the file can still continue inbuild settingsAdd configuration item in, will not overwrite;
1. View the configuration file

In fact, if you use Cocoapods to manage third-party libraries, you already automatically generate Debug and Release configuration files.

View Configurations: Xcode -> PROJECT -> Info -> Configurations

2. Customize the configuration file
  1. Commd +N– > searchConfiguration Setting FileSave the name as”Directory name -App name. The environment“For format, as in”Config-TestProject.debug“;
  2. in.xcconfigThe file writes the configuration, which is used herekeyActually all isbuild settingsAn abbreviation for configuration options, see the website for detailsXcode Build Settings;

For example, write the following configuration in the configuration file:

SLASH = /
HOST_URL = http:${SLASH}/1234
OTHER_LDFLAGS =  -framework "AFNetworking"
HEADER_SEARCH_PATHS = /abcd/efg
Copy the code

After compiling, Search for Other Linker Flags and Header Search Paths in build Settings. These Settings have been modified successfully.

3. Special symbol problem

When writing configuration information, the // of the URL is treated as a comment. At this point, we can define a variable SLASH = / as follows:

SLASH = /
HOST_URL = http:${SLASH}/5678
Copy the code
4. Add constraints

In some cases, we need to add constraints to the configuration. For example, the frameworkName is linked only in the Debug environment, the SDK is an emulator, and the framework is x86_64.

OTHER_LDFLAGS[config=Debug][sdk=iphonesimulator*][arch=x86_64] = -framework "frameworkName"
Copy the code
5. Xcconfig file conflicts

We are used to usingcocoapodsThe configuration file generated by the third party will conflict with our custom configuration file, because only one environment can be configured.xcconfigFile:

Also, updating Pods at this time will warn that other configuration files already exist, and the.xcconfig file set by Pods will not take effect;

Solution: Introduce the PODS profile to take effect using the key include command in a custom profile:

// Note: The Settings need to be set according to different environments such as debug and release
#include "Pods/Target Support Files/Pods-TestProject/Pods-TestProject.debug.xcconfig"
Copy the code
6. Xcconfig keyword conflict

Importing the.xcconfig files generated by Pods into a custom configuration file through include will cause the former to be invalid if both have configured the same key.

For example, both configure the same Other Link Flags, and when you look at Build Setting, the.xcconfig generated by Pods does not take effect.

The inherited configuration is as follows in the custom configuration file:

OTHER_LDFLAGS = $(inherited) -framework "AFNetworking"
Copy the code
7. Test. Xcconfig multiple environments
  1. In different.xcconfigDifferently definedHOST_URLAnd take steps similar to the above ininfo.plistTo add the configuration;
  2. Switch to a differentconfigrationRun the project below and you will get a differentHOST_URL;

Refer to the link

  1. This article tests the Demo address
  2. IOS multiple targets to develop similar apps