Share the hottest ios information
App development usually involves multiple environments, such as Debug and Release. The environment switchover may involve the switchover of the server url or the appid of some third-party sdks. Initially, I set the environment by adding variables to the code:
static let isRelease = true static let serverURL = isRelease ? "prod server url" : "dev server url"
However, the disadvantage of this is that you need to change this variable every time you switch the environment, and you will still be bored after multiple modifications. In addition, there may be more than two environments during development. Therefore, configuring app environment variables can better solve the problem of environment switching.
There are many ways to configure app environment variables. I choose a way that I feel is intuitive - using the xcconfig file.
demand
Xcode provides two configuration environments by default: Debug and Release. The differences between the two are:
Debug will provide more debugging information (many people on the Internet say release, and breakpoints are not allowed in the environment. After your own test, you find that breakpoints have nothing to do with debug and release, which will be explained in detail later)
Release runs much faster and smoothly. The package size may be smaller than the debug
Three environments are mainly used in my development:
The two provided by default are not enough...
According to the above requirements, the operation steps are roughly as follows:
1. Add Build Configuration
Open the workspace of the project and enter the main interface of xcode.
Select the project ->info of the main project, find Configurations, and click the "+" below
You can see from the figure that two configurations have been added: Debug and Release
Select "deploy debug configuration", add a configuration that looks like it, and name it ReleaseTest:
As can be seen from the figure, the three configurations already have a default configuration set (that is, xxconfig file). This is because my project has included cocoapods. The default configuration set is added by pods.
At this time, the build configuration added in the face does not have the xcconfig of the corresponding pods, so the project will report an error. Set the set corresponding to the configuration of the face sample to none
Command line running
pod install
After completion, see the figure below
2. Create and configure the xcconfig file as a face sample
Common+"n", select iOS ->Other ->Configuration settings file
Four xxconfig files are created as samples, and I use the following names:
Including:
CommonConfig.xcconfig file contains some general configurations, such as build version
The other three files correspond to three build configurations
Add in CommonConfig:
BUILD_VERSION = 1.0.0
DebugConfig:
/* Import public config */ #include "CommonConfig.xcconfig" /* Import config corresponding to pods */ #include "Pods/Target Support Files/Pods/Pods.debug.xcconfig" APP_DISPLAY_NAME=Test Service CONFIG_FLAG = DEBUG
ReleaseConfig:
#include "CommonConfig.xcconfig" #include "Pods/Target Support Files/Pods/Pods.release.xcconfig" APP_DISPLAY_NAME=Real Name CONFIG_FLAG = RELEASE
ReleaseTestConfig:
#include "CommonConfig.xcconfig" #include "Pods/Target Support Files/Pods/Pods.releasetest.xcconfig" APP_DISPLAY_NAME=Official service CONFIG_FLAG = RELEASE_TEST
Including:
#include "Pods/Target Support Files/Pods/Pods.releasetest.xcconfig"
The path may be different due to different project names. If you are not sure, you can pop install again. Pods will give a prompt, including the correct path.
[!] 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 configTest to Pods/Target Support Files/Pods/Pods.debug.xcconfig or include the Pods/Target Support Files/Pods/Pods.debug.xcconfig in your build configuration.
After the above is completed, switch the build configuration to the corresponding profile sample file:
3. Set environment variables
After completing the above steps, the environment has been added. The rest is to set environment variables
In the Info.plist file, set the Bundle name to
${APP_DISPLAY_NAME}
The name of the application will change the credential configuration.
However, what if you need the credential environment to change the value of some variables in your code?
1) Set precompiled header parameters
Objective-C
Project -> Build settings -> Apple LLVM 7.1 - Preprocessing
Add in preprocessor
${CONFIG_FLAG}=1
Swift
Project -> Build settings -> Swift Compiler - Custom Flags
Add in other swift flags
-D ${CONFIG_FLAG}
then
In the code:
struct AppConfig { private enum AppConfigType { case Debug case Release case ReleaseTest } private static var currentConfig: AppConfigType { #if DEBUG = 1 return . Debug #elseif RELEASE_TEST = 1 return . ReleaseTest #else return . Release #endif } static var webServerURL: String { switch currentConfig { case . Debug: return "test url" default: return "release url" } } }
Other variables can also be configured in the above way.
Add multiple schemes to facilitate configuration and switching
Change the build configuration in the scheme to achieve different environment switching, or add multiple schemes to achieve more convenient switching
The added face scheme needs to check shared in the management scheme. Other talents on the git can see the face scheme
DEMO and recommendation
XCConfig Demo
How to configure multiple environment variables for an iOS app
Use will to overcome body inertia!