xcode 我们如何为不同的环境(例如 Dev、Test、Staging 和 Prod)使用不同的 Info.plist 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21211267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:34:11  来源:igfitidea点击:

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

iosobjective-cxcode

提问by Yas T.

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

我们如何Info.plist为不同的环境(例如Dev、Test、Staging 和 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.

我为每个环境都有一些设置和一个单独的 Facebook 应用程序,以确保应用程序分析不会受到测试人员等的偏见。因此,在为每个环境构建之前,我真的尽量避免手动更新设置。

回答by Yas T.

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

这是添加特定于环境的 plist 所需的操作。

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

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

  • Type Info.plistin 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 Phasemenu option.

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

  • 正本ProjectName.Info.plist文件ProjectName_Dev.Info.plistProjectName_Test.Info.plist以及ProjectName_Staging.Info.plist和它们添加到项目中。

  • 单击 中的项目名称Project Navigator,选择Target,然后选择Build Phases选项卡。

  • 键入Info.plist在右上角的搜索栏来过滤上的Info.plist。

  • 从 下Copy Bundle Resources,删除所有 plist,除了ProjectName.Info.plist

  • 现在点击Editor -> Add Build Phase -> Add Run Script Build Phase菜单选项。

  • 最后,将以下 shell 脚本复制到新添加的 Build Phase 中。

Make sure to replace ProjectNamewith your project name!

确保替换ProjectName为您的项目名称!

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"

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.

注意:我假设您已经使用 Dev、Test、Staging 和 Production 环境变量创建了构建方案。

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

从这篇文章中得到了帮助。 http://www.dosomethinghere.com/2013/09/21/different-settings-app-entries-for-debug-vs-release-builds/

回答by bgfriend0

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:

您还可以为每个目标创建单独的 xcconfig 文件,使用项目管理器为每个目标分配正确的 xcconfig 文件,然后只需在每个 xcconfig 中定义一个具有相同名称的变量并将该变量导入到您的单个 plist 中。例如:

first xcconfig:

首先xcconfig:

MY_VARIABLE = suchandsuch

MY_VARIABLE = 如此如此

second xcconfig:

第二个 xcconfig:

MY_VARIABLE = thisandthat

MY_VARIABLE = 这个那个

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

然后在你的 plist 中,设置一个值为 $(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.

具体取决于你在做什么。Xcconfig 很好,因为除了 plist 之外,您还可以访问在构建设置等地方设置的变量。

回答by Vincil Bishop

Try this helper class:

试试这个助手类:

https://github.com/premosystems/MyEnvironmentConfig

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.

  1. 将环境 $(CONFIGURATION) 添加到 info.plist。

  2. 添加 Environments.plist 配置文件,填充您最喜欢的变量优点。

  3. 在 MYEnvironmentConfig 类上添加一个方便的类别,公开强类型的配置值。

  4. 在 appDidFinishLaunching 中初始化 MyEnvironmentConfig。