Use User-Defined build settings in custom .plist file
Create a new folder (for example:
GoogleServiceInfoPlists
).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
Add new
Run Script Phase
at last (Xcode: Target -> Build Phases -> "+" button).Use script below to copy
.plist
file for given environment to the build directory.
script:
RESOURCE_PATH=${SRCROOT}/${PRODUCT_NAME}/GoogleServiceInfoPlists/GoogleService-Info-$CONFIGURATION.plist
BUILD_APP_DIR=${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app
echo "Copying ${RESOURCE_PATH} to ${BUILD_APP_DIR}"
cp "${RESOURCE_PATH}" "${BUILD_APP_DIR}/GoogleService-Info.plist"
PS: You do not need to add the file to project. Just create a new folder in the main directory.
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:
Create a new folder (for example:
Resources/GoogleServiceInfoPlists
).Copy there all .plist files for build configuration. For example:
GoogleService-Info-Debug.plist
GoogleService-Info-Stage.plist
GoogleService-Info-Prod.plist
Add new
Run Script Phase
- Xcode: Target-->Build Phases-->"+" button (top left corner).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"
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.
"${SRCROOT}/${PRODUCT_NAME}/GoogleService-Info.plist"
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.
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.