“I walk slowly, but I never walk backward.”
I am a slow walker, but I never walk backwards
Main Contents:
- Distinguish between
Project
,Target
,Scheme
- more
Target
configuration Scheme
configurationxcconfig
Configuration 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 specifiedTarget
To 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:
- more
target
Configuration; Scheme
Configuration;xcconfig
File configuration;
2. Multi-target configuration
1. Summarize the characteristics
- Advantages: Easy to manage code, resource files, such as in
Compile Sources
,Copy Bundle Resources
To add or remove code and resources to compile as needed; - Disadvantages: The configuration is cumbersome and involves multiple configurations
info.plist
File, 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 originalTarget
New 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:
- Modify the new
Target
The name; - Modify the new
info.plist
The name; - Modify the
Build Settings
->Packaging
->info.plist File
与Product Bundle identifier
;
3. Change the Scheme name
Enter theScheme Manager
, modifyScheme
The name of the, here is not necessary, modified to make it easier to identify;
4. Add predefined macros to distinguish different targets
configurationOC
Engineering:Build Settings
-> Preprocessor Macros
configurationSwift
Engineering:Build Settings
-> Other Swift Flags
Understand -d
swiftc
In the compiler, you need to add one-D
theoption
, represents marking the conditional compilation flag astrue
;- As for the
-D
Can 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
increaseconfiguration
Later, inEdit Scheme
There will also be one moreconfigration
. We are inrun
“, you can manually switchconfigration
Use different environments;
3. The Scheme
Manual switching each timeconfigration
It’s a little bit more tedious, but we can use different onesScheme
Directly corresponding to different configurations;
4. Test Scheme multi-environment
1.Build Setting
-> User-Defined
In the customHost_URL
And then to differentconfigration
Configure different values;
2.Host_URL
Configure as a variableTo the info. The plist
File;
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:
.xcconfig
Configuration files, which help us to configure in the form of filesbuild settings
The contents of;.xcconfig
The content of the document iskey-value
2. Formed of form;- increase
.xcconfig
After the file can still continue inbuild settings
Add 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
Commd +N
– > searchConfiguration Setting File
Save the name as”Directory name -App name. The environment
“For format, as in”Config-TestProject.debug
“;- in
.xcconfig
The file writes the configuration, which is used herekey
Actually all isbuild settings
An 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 usingcocoapods
The configuration file generated by the third party will conflict with our custom configuration file, because only one environment can be configured.xcconfig
File:
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
- In different
.xcconfig
Differently definedHOST_URL
And take steps similar to the above ininfo.plist
To add the configuration; - Switch to a different
configration
Run the project below and you will get a differentHOST_URL
;
Refer to the link
- This article tests the Demo address
- IOS multiple targets to develop similar apps