Use User-Defined build settings in custom .plist file Use User-Defined build settings in custom .plist file ios ios

Use User-Defined build settings in custom .plist file

It's NOT possible to use User-Defined settings in custom .plist file.

BUT you can copy your custom .plist file to the right place when building the app:

  1. Create a new folder (for example: Resources/GoogleServiceInfoPlists).

  2. Copy there all .plist files for build configuration. For example:

  3. GoogleService-Info-Debug.plist

  4. GoogleService-Info-Stage.plist

  5. GoogleService-Info-Prod.plist

  6. Add new Run Script Phase - Xcode: Target-->Build Phases-->"+" button (top left corner).

  7. Use the script below to copy (replace) .plist file for given build configuration to the project directory:

    cp "${SRCROOT}/${PRODUCT_NAME}/Resources/GoogleServiceInfoPlists/GoogleService-Info-$CONFIGURATION.plist" "${SRCROOT}/${PRODUCT_NAME}/GoogleService-Info.plist"
  8. Under Output Files, you should possibly also enter the target file in order to ensure the next phase begins only if the file was actually copied.

  9. Important: move newly added script before Copy Bundle Resources phase, or it will use the default file, ignoring replaced file.

Used variables/paths:

${SRCROOT} - predefined, it points to your project location.

${PRODUCT_NAME} - predefined, product name.

$CONFIGURATION - predefined, it's your build configuration. By default, it is: Debug, Release. In my case: Debug, Stage, Prod. You can change build configurations in Xcode: Project (not Target!)-->Info.


GoogleService-Info.plist file must be added to the Xcode project resources (Build Phases-->Copy Bundle Resources) while Resources/GoogleServiceInfoPlists/GoogleService-Info-* files not necessarily.

If the above doesn't work try clicking in your disired .plist file > File Inspector > Target Membership > Check your main project so it can be bundled together in final output.

  1. Create a new folder (for example: GoogleServiceInfoPlists).

  2. Copy there all .plist files for each Configuration

for example:

GoogleService-Info-Debug.plist, GoogleService-Info-Alpha.plist,GoogleService-Info-Beta.plist,GoogleService-Info-Release.plist
  1. Add new Run Script Phase at last (Xcode: Target -> Build Phases -> "+" button).

  2. Use script below to copy .plist file for given environment to the build directory.



PS: You do not need to add the file to project. Just create a new folder in the main directory.

enter image description here

Actually there's a better solution.

You can replace Env variables(including User-Defined) manually using my swift script.

It takes two arguments: input and output paths. Goes through all input plist values recursively (if you need to can add dict keys to be processed too), and replaces all Env variables it founds. And writes to output path.

This is how you use it in your Run script phase:

/usr/bin/xcrun --sdk macosx swift "${PROJECT_DIR}/scripts/replacePlistEnvironmentVariables.swift"  "${PROJECT_DIR}/GoogleService-Info.plist" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/GoogleService-Info.plist"