xcode 在自定义 .plist 文件中使用用户定义的构建设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34067120/
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
Use User-Defined build settings in custom .plist file
提问by KlimczakM
I have different build configurations (Debug, Stage, Prod) defined for my app and I use User-Definedbuild settings:
我为我的应用程序定义了不同的构建配置(调试、阶段、生产),我使用用户定义的构建设置:
to set up Facebook login and other stuff in Info.plist
file:
在Info.plist
文件中设置 Facebook 登录和其他内容:
In this scenario the $(USER_DEFINED_SETTINGS)
notation doeswork.
在这种情况下,$(USER_DEFINED_SETTINGS)
符号确实有效。
When I tried to set up Google SignIn, which requires using additional .plist
file (GoogleService-Info.plist
), and I used User-Definedsettings in the same way I do in the Info.plist
file, it doesn't work.
当我尝试设置需要使用附加文件 ( ) 的Google SignIn并且我以与文件中相同的方式使用用户定义的设置时,它不起作用。.plist
GoogleService-Info.plist
Info.plist
How can I use User-Definedsettings in custom .plist
files? If I can't, how can I workaround this?
如何在自定义文件中使用用户定义的设置.plist
?如果我不能,我该如何解决这个问题?
回答by KlimczakM
It's NOTpossible to use User-Definedsettings in custom.plist file.
这是不是可以使用用户定义的设置,自定义的.plist文件。
BUTyou can copy your custom .plist file to the right place when building the app:
但是您可以在构建应用程序时将自定义 .plist 文件复制到正确的位置:
- Create a new folder (for example:
src/Resources/GoogleServiceInfoPlists
). - Copy there all .plist files for each environment. 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 environment to the main directory (it's
src
in my case, but usually it is a project name):cp "${SRCROOT}/src/Resources/GoogleServiceInfoPlists/GoogleService-Info-$CONFIGURATION.plist" "${SRCROOT}/src/GoogleService-Info.plist"
- 创建一个新文件夹(例如:)
src/Resources/GoogleServiceInfoPlists
。 - 复制每个环境的所有 .plist 文件。例如:
GoogleService-Info-Debug.plist
GoogleService-Info-Stage.plist
GoogleService-Info-Prod.plist
- 添加新的
Run Script Phase
(Xcode:目标-> 构建阶段->“+”按钮[左上角])。 使用下面的脚本将给定环境的 .plist 文件复制(替换)到主目录(
src
在我的情况下,但通常是项目名称):cp "${SRCROOT}/src/Resources/GoogleServiceInfoPlists/GoogleService-Info-$CONFIGURATION.plist" "${SRCROOT}/src/GoogleService-Info.plist"
Example result(for Stage environment):
示例结果(对于舞台环境):
File src/Resources/GoogleServiceInfoPlists/GoogleService-Info-Stage.plist
is copied to src/GoogleService-Info.plist
during the build.
文件在构建期间src/Resources/GoogleServiceInfoPlists/GoogleService-Info-Stage.plist
被复制到src/GoogleService-Info.plist
。
Used variables/paths:
使用的变量/路径:
${SRCROOT}
- predefined, it points to your project location.
${SRCROOT}
- 预定义,它指向您的项目位置。
$CONFIGURATION
- predefined, it's your build configuration. By default, it is: Debug
, Release
. In my case: Debug
, Stage
, Prod
. You can change this in Xcode: Project(not target!)->Info.
$CONFIGURATION
- 预定义,这是您的构建配置。默认情况下,它是:Debug
, Release
。就我而言:Debug
, Stage
, Prod
. 您可以在 Xcode 中更改此设置:Project(not target!)-> Info。
Important:
重要的:
src/GoogleService-Info.plist
file mustbe added to the Xcode project (Build Phases->Copy Bundle Resources) while/src/Resources/GoogleServiceInfoPlists/GoogleService-Info-*
files don't have to.Your new Run Script must be placed before Copy Bundle Resourcesbuild phase. Otherwise, it will be copied too late and the default version of the .plist file would be used.
src/GoogleService-Info.plist
文件必须添加到 Xcode 项目(Build Phases->Copy Bundle Resources),而/src/Resources/GoogleServiceInfoPlists/GoogleService-Info-*
文件则不必。您的新运行脚本必须放置在 Copy Bundle Resources构建阶段之前。否则,它会被复制太晚并且会使用 .plist 文件的默认版本。
回答by user2541964
Create a new folder (for example:
GoogleServiceInfoPlists
).Copy there all
.plist
files for each Configuration
创建一个新文件夹(例如:)
GoogleServiceInfoPlists
。复制
.plist
每个配置的所有文件
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.
Run Script Phase
最后添加新的(Xcode:目标 -> 构建阶段 -> “+”按钮)。使用下面的脚本
.plist
将给定环境的文件复制到构建目录。
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.
PS:您不需要将文件添加到项目中。只需在主目录中创建一个新文件夹。
回答by mevdev
I put two files with the (same) name GoogleService-Info.plistinto my project.
我将两个具有(相同)名称的文件GoogleService-Info.plist放入我的项目中。
One is at the root and one is in a folder called 'staging', so as to avoid a naming conflict in the file system.
一个位于根目录,一个位于名为“staging”的文件夹中,以避免文件系统中的命名冲突。
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.
在一个目标中包含一个,在另一个中包含另一个,这样每个目标都有一个具有正确名称的唯一 plist 文件。