Use User-Defined build settings in custom .plist file

KlimczakM picture KlimczakM · Dec 3, 2015 · Viewed 8.6k times · Source

I have different build configurations (Debug, Stage, Prod) defined for my app and I use User-Defined build settings:

enter image description here

to set up Facebook login and other stuff in Info.plist file:

enter image description here

In this scenario the $(USER_DEFINED_SETTINGS) notation does work.

When I tried to set up Google SignIn, which requires using additional .plist file (GoogleService-Info.plist), and I used User-Defined settings in the same way I do in the Info.plist file, it doesn't work.

How can I use User-Defined settings in custom .plist files? If I can't, how can I workaround this?

Answer

KlimczakM picture KlimczakM · Dec 3, 2015

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:
  • GoogleService-Info-Debug.plist
  • GoogleService-Info-Stage.plist
  • GoogleService-Info-Prod.plist
  1. Add new Run Script Phase - Xcode: Target-->Build Phases-->"+" button (top left corner).

  2. 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"
    
  3. 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.

Note:

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.