How can we have different Info.plist files for dif

2020-06-02 05:25发布

问题:

How can we have different Info.plist files for different environments such as Dev, Test, Staging, and Prod?

I have some settings and a separate Facebook app for each environments to ensure app analytics do not get biased by testers, etc. So really trying to avoid manually updating settings before building for each environment.

回答1:

Here is what you need to do to add environment specific plists.

  • Copy original ProjectName.Info.plist file to ProjectName_Dev.Info.plist, ProjectName_Test.Info.plist, and ProjectName_Staging.Info.plist and add them to the project.

  • Click on project name in the Project Navigator, select Target, then select Build Phases tab.

  • Type Info.plist in the search bar at top right to filter on Info.plist.

  • From under the Copy Bundle Resources, remove all plists except ProjectName.Info.plist

  • Now click on Editor -> Add Build Phase -> Add Run Script Build Phase menu option.

  • Finally, copy following shell script to the newly added Build Phase.

Make sure to replace ProjectName with your project name!

if [ "${CONFIGURATION}" == "Dev" ]; then
cp -r "${PROJECT_DIR}/ProjectName/ProjectName-Info_Dev.plist" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/ProjectName-Info.plist"
echo "DEV plist copied"
elif [ "${CONFIGURATION}" == "Test" ]; then
cp -r "${PROJECT_DIR}/ProjectName/ProjectName-Info_Test.plist" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/ProjectName-Info.plist"
echo "Beta plist copied"
elif [ "${CONFIGURATION}" == "Staging" ]; then
cp -r "${PROJECT_DIR}/ProjectName/ProjectName-Info_Staging.plist" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/ProjectName-Info.plist"
echo "Beta plist copied"
elif [ "${CONFIGURATION}" == "Prod" ]; then
cp -r "${PROJECT_DIR}/ProjectName/ProjectName-Info_Prod.plist" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/ProjectName-Info.plist"
echo "Beta plist copied"
fi

Or just:

cp -r "${PROJECT_DIR}/ProjectName/ProjectName-Info_${CONFIGURATION‌​}.plist" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/ProjectName-Info.‌​plist"

NOTE: I assume you already have created the build schemes with Dev, Test, Staging, and Production environment variables.

Got help from this article. http://www.dosomethinghere.com/2013/09/21/different-settings-app-entries-for-debug-vs-release-builds/



回答2:

You can also create separate xcconfig files for each target, use the project manager to assign each target the correct xcconfig file, and then just define a variable with the same name in each xcconfig and import that variable into your single plist. For example:

first xcconfig:

MY_VARIABLE = suchandsuch

second xcconfig:

MY_VARIABLE = thisandthat

And then in your plist, set a key with the value $(MY_VARIABLE)

Depends on what exactly you're doing. Xcconfig is nice because you can access the variables you set there in places like build settings in addition to plist.



回答3:

Try this helper class:

https://github.com/premosystems/MyEnvironmentConfig

  1. Add Environment $(CONFIGURATION) to info.plist.

  2. Add Environments.plist configuration file, populate with you favorite variable goodness.

  3. Add a convenience category on the MYEnvironmentConfig class exposing strongly typed configuration values.

  4. Initialize MyEnvironmentConfig in appDidFinishLaunching.