Use different GoogleService-Info.plist for different build schemes

后端 未结 17 718
梦毁少年i
梦毁少年i 2020-12-07 07:49

I am using a build scheme for prod and one for staging (with 2 different bundle identifiers) and I am trying to use a separate GoogleService-Info.plist for each scheme. Is t

17条回答
  •  -上瘾入骨i
    2020-12-07 08:26

    So I have pondered the same question and using some ideas from earlier posts, some of which publish apps with GoogleServices-Info.plist for all environments in all apps and that is a bit of a concern.

    I have come up with an extensible solution that copies the GoogleSerives-Info.plist file at build time. Further more this approach can support as many environments as you like with the ability to customise and follows a simple convention, making it easy to manage.

    First and foremost i have three environments, debug (For running in simulator and device whist debugging and actively cutting code), staging (for deployment to test flight) and release for production.

    Step one is to create your configuration(s):

    Select "Product" -> "Scheme" -> "Edit Scheme" and duplicate/create new as required. Go through each Scheme and assign its respective configuration from the "Build Configuration" drop down in each of the categories:

    I go a step further and uncheck "run" for Schemes that need to be distributed i.e. release and staging, and conversely uncheck "archive" for debug. You should do what makes sense for you.

    Under build phases add the following run scrip (CONFIGURATIONS_FOLDER variable can be customised as desired - just ensure you use the same folder name in the next step):

    # Get a reference to the folder which contains the configuration subfolders.
    CONFIGURATIONS_FOLDER=Firebase
    # Get a refernce to the filename of a 'GoogleService-Info.plist' file.
    GOOGLESERVICE_INFO_PLIST=GoogleService-Info.plist
    # Get a reference to the 'GoogleService-Info.plist' for the current configuration.
    GOOGLESERVICE_INFO_PLIST_LOCATION=${PROJECT_DIR}/${TARGET_NAME}/${CONFIGURATIONS_FOLDER}/${CONFIGURATION}/${GOOGLESERVICE_INFO_PLIST}
    # Check if 'GoogleService-Info.plist' file for current configuration exist.
    if [ ! -f $GOOGLESERVICE_INFO_PLIST_LOCATION ]
    then
      echo "No '${GOOGLESERVICE_INFO_PLIST}' file found for the configuration '${CONFIGURATION}' in the configuration directory '${PROJECT_DIR}/${TARGET_NAME}/${CONFIGURATIONS_FOLDER}/${CONFIGURATION}'."
      exit 1
    fi
    # Get a reference to the destination location for the GoogleService-Info.plist.
    GOOGLESERVICE_INFO_PLIST_DESTINATION=${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app
    # Copy 'GoogleService-Info.plist' for current configution to destination.
    cp "${GOOGLESERVICE_INFO_PLIST_LOCATION}" "${GOOGLESERVICE_INFO_PLIST_DESTINATION}"
    echo "Successfully coppied the '${GOOGLESERVICE_INFO_PLIST}' file for the '${CONFIGURATION}' configuration from '${GOOGLESERVICE_INFO_PLIST_LOCATION}' to '${GOOGLESERVICE_INFO_PLIST_DESTINATION}'."
    

    In your chosen configurations folder ("Firebase" in the above example) nest folders for each configuration named exactly the same as its respective configuration (case sensitive), inside of which place the respective GoogleServices-Info.plist files like so:

    Last but not least, i also like to ensure that a root level GoogleServices-Info.plist is not added into the project by accident so I add the following to my .gitignore.

    # Ignore project level GoogleService-Info.plist
    /[Project Name]/GoogleService-Info.plist
    

提交回复
热议问题