background
In application development, our process is: development-test-pre-release-release AppStrore
So there will be different environments, such as development environment, test environment, pre-release environment and formal environment.
Then there will be the following requirements for configuring different environments.
- Set different hosts and application icons for different environments (optional)
- Ability to quickly view information such as the environment, version number, build number, etc.
- Able to cooperate with automatic packaging
According to whether you need to compile and repackage, it can be divided into the following two ways
- Dynamic switching within the app
- Multi-environment configuration packaging and compilation switching
Dynamic switching within the app
In this way, we can create a shadow entry and switch the environment in the shadow entry page (at the same time, we can also display the environment, version number, build number and other information on the page).
Pros: No need to repackage.
The principle here mainly relies on code logic and localization records to achieve switching.
Multi-environment configuration package compilation
We configure the configuration files (Configuration Setting file) of multiple environments, and select the corresponding configuration by compiling and packaging to achieve functions such as switching environments and changing application icons.
Here I use the form of multiple Schemes + ConfigurationSettingFile .
Add Configuration
The Xcode project has two configurations of Debug and Release by default. Obviously not enough.
First open the project in Xcode, select PROJECT, selectInfo
Find Configurations
and click at the bottom +
, and then make a copy of Debug.
As shown below:
In the same way, we created Test and PreRelease
User-Defined
User-Defined
You can customize parameters in Build Setting, and you can click +
to add custom parameters for use in Info.plist or in compilation.
The key-value added in the configuration file is equivalent to adding User-Defined
parameters (that is, the key-value in the final configuration file will be synchronized here)
The use of User-Defined can be used for iOS development, using Build Configuration + Schemes multi-environment configuration 2. Create User-Defined Setting]
Note: User-Defined is divided into PROJECT and TARGETS levels, it is recommended to add in the corresponding TARGET
Addition Configuration Setting File
Configuration Setting File is for better management of configuration items, avoiding parameters scattered in Build Setting.
Configuration Setting files are inheritable. It is recommended to set up a ``Base`Configuration Setting file.
Configuration Setting File can be created by creating a new file
Here I created a base
Configuration Setting file corresponding to the other four environments.
The next step is to match the corresponding configuration files for each of your environments.
If you use Cocoapods.
Unable to load contents of file list: '/Target Support Files/Pods-AnyTest/Pods-AnyTest-frameworks-PreRelease-input-files.xcfilelist'
You may encounter errors when compiling .
At this time, you need to re-execute in the terminalpod install
After execution, you may encounter the following warning
[!] CocoaPods did not set the base configuration of your project because your project already has a custom config set. In order for CocoaPods integration to work at all, please either set the base configurations of the target `AnyTest` to `Target Support Files/Pods-AnyTest/Pods-AnyTest.debug.xcconfig` or include the `Target Support Files/Pods-AnyTest/Pods-AnyTest.debug.xcconfig` in your build configuration (`AnyTest/Config/Config.debug.xcconfig`).
[!] CocoaPods did not set the base configuration of your project because your project already has a custom config set. In order for CocoaPods integration to work at all, please either set the base configurations of the target `AnyTest` to `Target Support Files/Pods-AnyTest/Pods-AnyTest.test.xcconfig` or include the `Target Support Files/Pods-AnyTest/Pods-AnyTest.test.xcconfig` in your build configuration (`AnyTest/Config/Config.test.xcconfig`).
[!] CocoaPods did not set the base configuration of your project because your project already has a custom config set. In order for CocoaPods integration to work at all, please either set the base configurations of the target `AnyTest` to `Target Support Files/Pods-AnyTest/Pods-AnyTest.release.xcconfig` or include the `Target Support Files/Pods-AnyTest/Pods-AnyTest.release.xcconfig` in your build configuration (`AnyTest/Config/Config.release.xcconfig`).
[!] CocoaPods did not set the base configuration of your project because your project already has a custom config set. In order for CocoaPods integration to work at all, please either set the base configurations of the target `AnyTestTests` to `Target Support Files/Pods-AnyTestTests/Pods-AnyTestTests.prerelease.xcconfig` or include the `Target Support Files/Pods-AnyTestTests/Pods-AnyTestTests.prerelease.xcconfig` in your build configuration (`AnyTest/Config/Config.prerelease.xcconfig`).
Take it easy, we're going to fix it now
Now open, our Config.debug configuration file must inherit the corresponding environment configuration file of Cocoapods so that Cocoapods can run correctly.
Compile now and it will compile successfully.
In the same operation, we add the pod inheritance of other environments accordingly.
Create Scheme
In order for us to quickly select the compilation and running environment, we should create a Scheme corresponding to the environment.
Select the project -> New Scheme, create
The default created Scheme cannot be renamed, you may delete it and create a new named Scheme
As shown in the figure below, the Scheme corresponding to the environment is created.
The Build Configuration of the newly created Scheme is based on Debug by default. So we need to edit each Scheme to ensure that it corresponds to the compilation configuration one by one.
Add configuration parameters
Define public parameters in base for public control
Config.base configuration file example
APP_NAME = AnyTest // 应用名
APP_ICON = AppIcon // 图标名
BUNDLE_ID = com.xxx.AnyTest //包名
HOST_URL = www.base.com // 后端地址
Config.debug configuration file example
#include "Config.base.xcconfig" // 继承 base,以便公共控制
#include "Pods/Target Support Files/Pods-AnyTest/Pods-AnyTest.debug.xcconfig" // 继承 cocopods 以便其正常工作
APP_NAME = ${inherited}Debug // = AnyTestDebug
APP_ICON = ${inherited}Debug //= AppIconDebug
BUNDLE_ID = ${inherited}.Debug //= com.xxx.AnyTest.debug
HOST_URL = www.debug.com
The other three files are similar to Config.debug to set.
Host acquisition in different environments
Get it in info.plist by adding a custom field
Add a field in target info Host_URL
and set the value to$(HOST_URL)
Get it by reading info.plist in the code
func readHostURLFromInfoPlist() -> String? {
guard let path = Bundle.main.path(forResource: "Info", ofType: "plist") else {
return nil
}
let infoDic = NSDictionary(contentsOfFile: path)
let host = infoDic?["Host_URL"] as? String
return host
}
If you don't want to configure the host url by adding a Configuration Setting File, you can also define tags for different environments through target -> build setting -> Preprocessor Macros, and distinguish the host by conditional compilation in the code
Set application names for different environments
Similarly, by modifying the application name field in info.plist to the Key defined in the configuration file
Note: The application name is not the Bundle name haha
Set different environment package names
Similarly, modify the key in the info.plist file to the key in the configuration file
tip: If you generate different package names, you will generate different applications.
Set of different environment icons
In order to be able to see the icon and know what environment it is, you can add a watermark to the design to achieve what you see is what you get.
It is also possible to set the corresponding Key in the info.plist as the configuration file definition Key
How to change the app's icon at runtime
You ImageMagic
can to dynamically update the icon, and write important information (environmental watermark, version, build version, etc.) on the icon.
Refer to How to Change the App Icon at Runtime or Add Version Information to the iOS App Icon
Sample source code https://github.com/CodeOcenS/multiConfig
expand
Xcode Build Settings
Contains Xcode compilation parameters and various explanations and definitions, which can be queried when writing scripts