I have different build configurations (Debug, Stage, Prod) defined for my app and I use User-Defined build settings:
to set up Facebook log
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.
I put two files with the (same) name GoogleService-Info.plist into my project.
One is at the root and one is in a folder called 'staging', so as to avoid a naming conflict in the file system.
Including one in one target and the other in another makes it so that each target has a unique plist file with the correct name.
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"
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:
Resources/GoogleServiceInfoPlists
).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"
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.