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

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

Use User-Defined build settings in custom .plist file

iosxcodeplistgoogle-signin

提问by KlimczakM

I have different build configurations (Debug, Stage, Prod) defined for my app and I use User-Definedbuild settings:

我为我的应用程序定义了不同的构建配置(调试、阶段、生产),我使用用户定义的构建设置:

enter image description here

在此处输入图片说明

to set up Facebook login and other stuff in Info.plistfile:

Info.plist文件中设置 Facebook 登录和其他内容:

enter image description here

在此处输入图片说明

In this scenario the $(USER_DEFINED_SETTINGS)notation doeswork.

在这种情况下,$(USER_DEFINED_SETTINGS)符号确实有效。

When I tried to set up Google SignIn, which requires using additional .plistfile (GoogleService-Info.plist), and I used User-Definedsettings in the same way I do in the Info.plistfile, it doesn't work.

当我尝试设置需要使用附加文件 ( ) 的Google SignIn并且我以与文件中相同的方式使用用户定义的设置时,它不起作用。.plistGoogleService-Info.plistInfo.plist

How can I use User-Definedsettings in custom .plistfiles? 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 文件复制到正确的位置:

  1. Create a new folder (for example: src/Resources/GoogleServiceInfoPlists).
  2. Copy there all .plist files for each environment. For example:
    • GoogleService-Info-Debug.plist
    • GoogleService-Info-Stage.plist
    • GoogleService-Info-Prod.plist
  3. Add new Run Script Phase(Xcode: Target->Build Phases->"+" button[top left corner]).
  4. Use the script below to copy (replace) .plist file for given environment to the main directory (it's srcin my case, but usually it is a project name):

    cp "${SRCROOT}/src/Resources/GoogleServiceInfoPlists/GoogleService-Info-$CONFIGURATION.plist" "${SRCROOT}/src/GoogleService-Info.plist"
    
  1. 创建一个新文件夹(例如:)src/Resources/GoogleServiceInfoPlists
  2. 复制每个环境的所有 .plist 文件。例如:
    • GoogleService-Info-Debug.plist
    • GoogleService-Info-Stage.plist
    • GoogleService-Info-Prod.plist
  3. 添加新的Run Script Phase(Xcode:目标-> 构建阶段->“+”按钮[左上角])。
  4. 使用下面的脚本将给定环境的 .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.plistis copied to src/GoogleService-Info.plistduring 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:

重要的:

  1. src/GoogleService-Info.plistfile mustbe added to the Xcode project (Build Phases->Copy Bundle Resources) while /src/Resources/GoogleServiceInfoPlists/GoogleService-Info-*files don't have to.

  2. 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.

  1. src/GoogleService-Info.plist文件必须添加到 Xcode 项目(Build Phases->Copy Bundle Resources),而/src/Resources/GoogleServiceInfoPlists/GoogleService-Info-*文件则不必。

  2. 您的新运行脚本必须放置在 Copy Bundle Resources构建阶段之前。否则,它会被复制太晚并且会使用 .plist 文件的默认版本。

回答by user2541964

  1. Create a new folder (for example: GoogleServiceInfoPlists).

  2. Copy there all .plistfiles for each Configuration

  1. 创建一个新文件夹(例如:)GoogleServiceInfoPlists

  2. 复制.plist每个配置的所有文件

for example:

例如:

GoogleService-Info-Debug.plist, 
GoogleService-Info-Alpha.plist,
GoogleService-Info-Beta.plist,
GoogleService-Info-Release.plist
  1. Add new Run Script Phaseat last (Xcode: Target -> Build Phases -> "+" button).

  2. Use script below to copy .plistfile for given environment to the build directory.

  1. Run Script Phase最后添加新的(Xcode:目标 -> 构建阶段 -> “+”按钮)。

  2. 使用下面的脚本.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:您不需要将文件添加到项目中。只需在主目录中创建一个新文件夹。

enter image description here

在此处输入图片说明

回答by mevdev

I put two files with the (same) name GoogleService-Info.plistinto my project.

我将两个具有(相同)名称的文件GoogleService-Info.plist放入我的项目中。

enter image description here

在此处输入图片说明

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”的文件夹中,以避免文件系统中的命名冲突。

Include the plist in only one project each

将 plist 仅包含在一个项目中

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 文件。