Use User-Defined build settings in custom .plist file

后端 未结 4 1596
感动是毒
感动是毒 2020-12-24 14:22

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

to set up Facebook log

相关标签:
4条回答
  • 2020-12-24 14:48
    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.

    0 讨论(0)
  • 2020-12-24 14:49

    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.

    0 讨论(0)
  • 2020-12-24 15:03

    Actually there's a better solution.

    You can replace Env variables(including User-Defined) manually using my swift script.

    It takes two arguments: input and output paths. Goes through all input plist values recursively (if you need to can add dict keys to be processed too), and replaces all Env variables it founds. And writes to output path.

    This is how you use it in your Run script phase:

    /usr/bin/xcrun --sdk macosx swift "${PROJECT_DIR}/scripts/replacePlistEnvironmentVariables.swift"  "${PROJECT_DIR}/GoogleService-Info.plist" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/GoogleService-Info.plist"
    
    0 讨论(0)
  • 2020-12-24 15:06

    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: Resources/GoogleServiceInfoPlists).
    2. Copy there all .plist files for build configuration. For example:
    • GoogleService-Info-Debug.plist
    • GoogleService-Info-Stage.plist
    • GoogleService-Info-Prod.plist
    1. Add new Run Script Phase - Xcode: Target-->Build Phases-->"+" button (top left corner).

    2. Use the script below to copy (replace) .plist file for given build configuration to the project directory:

       cp "${SRCROOT}/${PRODUCT_NAME}/Resources/GoogleServiceInfoPlists/GoogleService-Info-$CONFIGURATION.plist" "${SRCROOT}/${PRODUCT_NAME}/GoogleService-Info.plist"
      
    3. Important: move newly added script before Copy Bundle Resources phase, or it will use the default file, ignoring replaced file.

    Used variables/paths:

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

    ${PRODUCT_NAME} - predefined, product name.

    $CONFIGURATION - predefined, it's your build configuration. By default, it is: Debug, Release. In my case: Debug, Stage, Prod. You can change build configurations in Xcode: Project (not Target!)-->Info.

    Note:

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

    0 讨论(0)
提交回复
热议问题