Use User-Defined build settings in custom .plist file

杀马特。学长 韩版系。学妹 提交于 2019-11-28 21:58:31

问题


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

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

In this scenario the $(USER_DEFINED_SETTINGS) notation does work.

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

How can I use User-Defined settings in custom .plist files? If I can't, how can I workaround this?


回答1:


It's NOT possible to use User-Defined settings in custom .plist file.

BUT you can copy your custom .plist file to the right place when building the app:

  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 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"
    

Example result (for Stage environment):

File src/Resources/GoogleServiceInfoPlists/GoogleService-Info-Stage.plist is copied to src/GoogleService-Info.plist during the build.

Used variables/paths:

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

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

Important:

  1. src/GoogleService-Info.plist file must be 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 Resources build phase. Otherwise, it will be copied too late and the default version of the .plist file would be used.




回答2:


  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.




回答3:


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.



来源:https://stackoverflow.com/questions/34067120/use-user-defined-build-settings-in-custom-plist-file

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