Use User-Defined build settings in custom .plist file

安稳与你 提交于 2019-11-30 00:15:15

It's NOT possible to use User-Defined settings in custom .plist file, so you have to handle this in the other way.

Although, you can copy your custom .plist file to the right place:

  1. Create a new folder (for example: GoogleServiceInfoPlists).
  2. Copy there all .plist files for each environment (for example: GoogleService-Info-Debug.plist, GoogleService-Info-Stage.plist and GoogleService-Info-Prod.plist).
  3. Add new Run Script Phase (Xcode: Target->Build Phases->"+" button).
  4. Use script below to copy (replace) .plist file for given environment to the main directory (it's src in my case):

    cp "${SRCROOT}/src/Resources/GoogleServiceInfoPlists/GoogleService-Info-$CONFIGURATION.plist" "${SRCROOT}/src/GoogleService-Info.plist"
    

${SRCROOT} - predefined, it points to your project location.

$CONFIGURATION - predefined, it's your build configuration, in my case: Debug, Stage, Prod. You can change this in Xcode: Project (not target!)->Info.

Please note that src/GoogleService-Info.plist file must be added to the Xcode project (Build Phases->Copy Bundle Resources) while /src/Resources/GoogleServiceInfoPlists/GoogleService-Info-* files not necessarily.

UPDATE:

Remember that your new Run Script must be placed before Copy Bundle Resources build phase. Otherwise, it won't work because it would be copied too late and the default version of the .plist file would be used.

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

  2. Copy there all .plist files for each Configuration

for example:

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

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

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.

mevdev

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

One is at the root and one is in a folder called 'staging', so as to avoid a naming conflict in the file system.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!