Hi everyone, in this article I will share how I have learned and applied it in projects that need to integrate Google service into different environments such as Development, Staging, Production. Hope this article will help you save time when needed. Currently, each environment I see will need a different bundle id, corresponding to google services, we will also need different GoogleService-Info.plist files. Below I will guide you to integrate it into the desired environment.
Create folder
First, we need to open the folder containing the project. Here we will need to create a directory to contain the GoogleService-Info.plist file. I create folders and subdivision as shown below:
Add the folder to the project
After preparing the corresponding GoogleService-Info.plist folders and files. Open your project, next, right click on the folder containing the newly created folder -> Add files to “…..” Note select: Create folder reference
Config script
Next, we will need to find the Build phases tab. Here we need to create New Run Script Phase . Here I will name it Config Google Service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# Xác định tên của file GOOGLESERVICE_INFO_PLIST=GoogleService-Info.plist # Xác định đương dẫn của file tương ứng cho các môi trường (Dev, staging, production) GOOGLESERVICE_INFO_DEV=${PROJECT_DIR}/${TARGET_NAME}/GoogleService/Dev/${GOOGLESERVICE_INFO_PLIST} GOOGLESERVICE_INFO_STAGING=${PROJECT_DIR}/${TARGET_NAME}/GoogleService/Staging/${GOOGLESERVICE_INFO_PLIST} GOOGLESERVICE_INFO_PRODUCTION=${PROJECT_DIR}/${TARGET_NAME}/GoogleService/Production/${GOOGLESERVICE_INFO_PLIST} # Kiểm tra xem có tồn tại file cho môi trương dev hay ko? echo "Looking for ${GOOGLESERVICE_INFO_PLIST} in ${GOOGLESERVICE_INFO_DEV}" if [ ! -f $GOOGLESERVICE_INFO_DEV ] then echo "No Development GoogleService-Info.plist found. Please ensure it's in the proper directory." exit 1 fi # Kiểm tra xem có tồn tại file cho môi trương staging hay ko? echo "Looking for ${GOOGLESERVICE_INFO_PLIST} in ${GOOGLESERVICE_INFO_STAGING}" if [ ! -f $GOOGLESERVICE_INFO_STAGING ] then echo "No STAGING GoogleService-Info.plist found. Please ensure it's in the proper directory." exit 1 fi # Kiểm tra xem có tồn tại file cho môi trương production hay ko? echo "Looking for ${GOOGLESERVICE_INFO_PLIST} in ${GOOGLESERVICE_INFO_PRODUCTION}" if [ ! -f $GOOGLESERVICE_INFO_PRODUCTION ] then echo "No Production GoogleService-Info.plist found. Please ensure it's in the proper directory." exit 1 fi # chỉ ra vị trí sẽ copy file đến. PLIST_DESTINATION=${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app echo "Will copy ${GOOGLESERVICE_INFO_PLIST} to final destination: ${PLIST_DESTINATION}" # kiểm tra môi trường tương ứng để tiến hành copy file đến vị trí chính xác if [ "${CONFIGURATION}" == "Staging" ] || [ "${CONFIGURATION}" == "StagingRelease" ] then echo "Using ${GOOGLESERVICE_INFO_STAGING}" cp "${GOOGLESERVICE_INFO_STAGING}" "${PLIST_DESTINATION}" elif [ "${CONFIGURATION}" == "Production" ] || [ "${CONFIGURATION}" == "ProductionRelease" ] then echo "Using ${GOOGLESERVICE_INFO_PRODUCTION}" cp "${GOOGLESERVICE_INFO_PRODUCTION}" "${PLIST_DESTINATION}" else echo "Using ${GOOGLESERVICE_INFO_DEV}" cp "${GOOGLESERVICE_INFO_DEV}" "${PLIST_DESTINATION}" fi |
In the above code, I have noted the steps in the script that will be executed, you just need to spend some time reading to understand how the code works, in addition, you can change it to fit. your project. Hopefully this article will be able to help you not only in configing google service but also in other cases.