Source code

home page  »  file  »  Source code  »  Configure iOS app environment variables with xcconfig file - iOS learning from beginner to proficient Ji Changxin

Configure iOS app environment variables with xcconfig file - iOS learning from beginner to proficient Ji Changxin

Share the hottest ios information

 img (178).jpg

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:

  • Test server debug

  • Occasionally switch to the official server debug

  • Release

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

 1471233137691898.png

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:

 1471233158568962.png

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

 1471233178761034.png

Command line running

 pod install

After completion, see the figure below

 1471233209126157.png

2. Create and configure the xcconfig file as a face sample

Common+"n", select iOS ->Other ->Configuration settings file

 QQ screenshot 20160815115345.png

Four xxconfig files are created as samples, and I use the following names:

 QQ screenshot 20160815115417.png

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:

 QQ screenshot 20160815115510.png

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.

 QQ screenshot 20160815120229.png

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

 QQ screenshot 20160815120203.png

Swift

Project -> Build settings -> Swift Compiler - Custom Flags

Add in other swift flags

 -D ${CONFIG_FLAG}

 QQ screenshot 20160815120211.png

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

 1471234018769574.png

 1471234034113101.png

Change the build configuration in the scheme to achieve different environment switching, or add multiple schemes to achieve more convenient switching

 QQ screenshot 20160815120637.png

 QQ screenshot 20160815120645.png

The added face scheme needs to check shared in the management scheme. Other talents on the git can see the face scheme

 QQ screenshot 20160815120758.png

DEMO and recommendation

XCConfig Demo

How to configure multiple environment variables for an iOS app

Use will to overcome body inertia!

fabulous ( zero )

This article is written by Ji Changxin Author, article address: https://blog.isoyu.com/archives/1030.html
use Knowledge Sharing Attribution 4.0 International License Agreement. Unless the reprint/source is indicated, they are all original or translated by this website. Please sign your name before reprinting. Last editing time: August 15, 2016 at 03:59 PM

key word:

Popular articles

Post reply

[Required]

I am a human?

Please wait three seconds after submission to avoid unsubmission and repetition