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
How can we have different Info.plist files for different environments such as Dev, Test, Staging, and Prod?
提问by Yas T.
How can we have different Info.plist
files 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.plist
file toProjectName_Dev.Info.plist
,ProjectName_Test.Info.plist
, andProjectName_Staging.Info.plist
and add them to the project.Click on project name in the
Project Navigator
, selectTarget
, then selectBuild 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 exceptProjectName.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.
正本
ProjectName.Info.plist
文件ProjectName_Dev.Info.plist
,ProjectName_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 ProjectName
with 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
Add Environment $(CONFIGURATION) to info.plist.
Add Environments.plist configuration file, populate with you favorite variable goodness.
Add a convenience category on the MYEnvironmentConfig class exposing strongly typed configuration values.
Initialize MyEnvironmentConfig in appDidFinishLaunching.
将环境 $(CONFIGURATION) 添加到 info.plist。
添加 Environments.plist 配置文件,填充您最喜欢的变量优点。
在 MYEnvironmentConfig 类上添加一个方便的类别,公开强类型的配置值。
在 appDidFinishLaunching 中初始化 MyEnvironmentConfig。