Harry Blue
Harry Blue

Reputation: 4502

Use .xcconfg values in .plist file

We use a library that requires config values to be set in an Okta.plist file.

As our app has 3 environments, we have config per environment within .xcconfig files.

This allows us to set things like app icon, app name etc etc depending on the build type (dev, test, release).

I'd like to use those values within Okta.plist also, so I can configure the instance of Okta we use, however I can't seem to set those values using the config file.

In the case of our Info.plist we simple reference the value:

/// Release.xcconfig
...
APP_DISPLAY_NAME = Some Text
...
/// Info.plist
<key>CFBundleDisplayName</key>
<string>${APP_DISPLAY_NAME}</string>

And it works.

How can I achieve the same with Okta.plist? I tried the same approach however it use the key as the value, not the value from the config file.

Upvotes: 3

Views: 897

Answers (1)

Christos Koninis
Christos Koninis

Reputation: 1688

All build settings(as declared in xcconfig files) are available as environment variables during the build phase script. Referencing those is only supported in info.plist, if you what to do this in any plist you must write a custom script that will perform the replacement and add it as a build phase script. You can use the following script Replaceplistenvironmentvariables.swift. This script takes an input & an output plist and will replace all reference to environment variables in the input plist with their value and write to the output plist.

Variable references must be in the following form:

<string>$(A_VARIABLE)</string> 

To add this to our project follow the steps

  • Download the Replaceplistenvironmentvariables.swift and place it in the top level of your project (no need to add a reference to the Xcode project)
  • Add a new "run script phase" in the build phases
  • Add the following line as a script

/usr/bin/xcrun --sdk macosx swift "${PROJECT_DIR}/replacePlistEnvironmentVariables.swift" "${SCRIPT_INPUT_FILE_0}" "${SCRIPT_OUTPUT_FILE_0}"

  • For "Input files" add the path as "User-Defined Setting" e.g. SCRIPT_INPUT_FILE_0=$(SRCROOT)/MyProject/Config.plist
  • For "Output files" add SCRIPT_OUTPUT_FILE_0=${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/Config.plist
  • Make sure that your plist (e.g. Config.plist) is not added to your target, because the Xcode will complain two build commands produce the same output.

Upvotes: 2

Related Questions