The basic concept

  • Project: contains all the code, resource files, and information of the Project.
  • Target: The specific build of the specified code and resource files.
  • Scheme: Configures the environment for the specified Target.

Configure multiple environments in different ways

Configure this parameter in Duplicate Target mode

  • This generates an additional target and an additional info.plist

  • You can define a macro way to do some differential configuration of the code
  • Oc inbuild setting->Preprocessor Macros
  • Swift inbuild setting->Other Swift Flags, it should be noted thatswiftTo use macros, add-Dparameter
  • This configuration has disadvantages
    • Generate multiple Info.plists
    • There are many points that need to be configured

Through Scheme configuration

  • throughEdit Scheme->Manage SchemesAdded schemes for different environments
  • Again will be differentSchemeCorresponds to different delta thetaBuild Configurationmode

Application Scenario Example

  • In daily development, in different environmentshost urlAddresses will vary by way of defining a macro

  • throughinfo.plistFiles come to light

  • Use as follows
NSString *path = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@".plist"];
NSDictionary *infoDict = [[NSDictionary alloc] initWithContentsOfFile:path];
NSLog(@"host url:%@",infoDict[@"HOST_URL"]);
Copy the code

Xcconfig file

Xcconfig guide

  • The syntax of the XCConfig file is relatively simple, each configuration file consists of a series of key-value assignments with the following syntax:
BUILD_SETTING_NAME = value
Copy the code
  • Build SettingValue Specifies the abbreviation of the field.Build Setting field corresponds to the query

annotation

  • The xcconfig file has only one comment method \.

Include Imports other Settings

  • As you create the XCConfig file, you can create as many as you need. This means that you can import configuration from other XCConfig using the include keyword. Enclose double quotes after the include keyword:
#include "Debug.xcconfig"
Copy the code
  • When searching for imported files, a slash is the absolute path, for example:
#include "/Users/xx/Desktop/... /xxx.xcconfig"Copy the code
  • Or through relative paths, starting with the ${SRCROOT} path:
#include "Pods/Target Support Files/xxx.xcconfig"
Copy the code

variable

  • Variable definitions, as per OC naming rules, consist only of uppercase letters, digits, and underscores(_)Composition, capital in principle, but also not. Strings can be"It can also be'Number.
  • There are three special cases of variables:
      1. inxcconfigThe variables defined inBuild SettingsIs consistent, then coverage will occur. Can be achieved by$(inherited), the current variable inherits the original value of the variable. Such as:
    OTHER_LDFLAGS = -framework SDWebImage OTHER_LDFLAGS = $(inherited) -framework AFNetworking // OTHER_LDFLAGS = -framework  SDWebImage -framework AFNetworkingCopy the code
    Note ⚠️ : Some variables cannot be configured to Build Settings via xcConfig. For example, PRODUCT_BUNDLE_IDENTIFIER does not work.Copy the code
      1. Reference variable,The $()andThe ${}Either way you can write it:VALUE=value
    TEACHER=$(VALUE)-${VALUE}
    Copy the code
      1. The conditional variable, according toSDK,ArchandConfigrationConditionalize Settings, for example:
    // Specify 'Configration' is' Debug '// specify' SDK 'is emulator, // Inherited =x86_64 OTHER_LDFLAGS[config=Debug][SDK =iphonesimulator* -framework "Cat"Copy the code
    Note ⚠️ : in Xcode 11.4 and later, you can use default to specify the default value for null variables: $(BUILD_SETTING_NAME:default=value)Copy the code

Priority (from highest to lowest) :

    1. Manually configure Target Build Settings
    1. The xcConfig file configured in Target
    1. Manually configure Project Build Settings
    1. The xcConfig file configured in Project