Fire base Environment

Multiple Firebase Environments(GoogleService-Info.plist) in iOS

A common scenario in application development is to have different environments, such as DebugQA, Production… can be resolve via Multiple Firebase Environments.

Requirement of having Multiple Firebase Environments :

While your app is in development mode, or while it is being tested by the QA department, some “data pollution” could reach Firebase servers.

Imagine your application is going to use Firebase Analytics and Firebase Realtime Database. The QA has received your build and is going to try it. They will navigate through the app, make taps on buttons, insert data on the database… You are sending “fake information” to Firebase, and you don’t want to mix this pollution data with the production database.

In other words, you could be mixing events from your testers which you only want for testing purposes, with end-users information which you do want for the correct behavior of your application. The solution consists of having different environments in your application, so that information from each one gets isolated and is not mixed with the others.

Setting up Firebase environments in XCode 

  1. In the Firebase console, create two separate projects (not applications): one will be for debug environment and the other for the release one. Name them appropriately so that you don’t confuse them.
  2. Create an iOS app for each project. Be careful to enter the bundle id you assigned to each project in xcode. 
  3. Download the GoogleService-Info.plist generated by Firebase for each project. By now, save them in different locations on your hard drive.  
  4. In xcode project navigator, create a new group and name it “Firebase”. 
  5. Inside “Firebase” directory, create two child directories named “Debug” and “Release” 
  6. In the project navigator, right-click on the new “Debug” folder and select “Add Files to…”. 
  7. You will come up with a new window. Navigate to the location of your debug plist file and select it. Make sure to uncheck “Add to targets” options and that “Copy items if needed” and “Create groups” are checked. 
  8. Repeat the last step for the release plist file, adding it to “Release” folder. So far, your project navigator should look like this: If you accidentally did not uncheck the “Add to targets” option, click on the plist file and uncheck the “Target Membership” option, located on the file inspector, at the right side of xcode. You may be asking why we have to uncheck this option. The reason is that, at compile-time, we will tell Xcode which of the two files it must copy to the final app bundle. If both files were checked to be in the app bundle, an error will raise because they are named the same. 
  9. In the project navigator, select the app target, switch to the “Build phases” tab, click the plus sign at the top and select New Run Script Phase. 
  10. Name the phase “Setup Firebase plist files” and add it below the “Link Binary with Libraries” step. 
  11. Create a new shell script that will copy the appropriate list file to the app bundle based on the current build configuration selected. Copy and paste the following code into the run script phase you just created: 
    # Name of the resource to copy
    INFO_PLIST_FILE=GoogleService-Info.plist
    
    # Get references to debug and release versions of the plist file
    DEBUG_INFO_PLIST_FILE=${PROJECT_DIR}/${TARGET_NAME}/Firebase/Debug/${INFO_PLIST_FILE}
    RELEASE_INFO_PLIST_FILE=${PROJECT_DIR}/${TARGET_NAME}/Firebase/Release/${INFO_PLIST_FILE}
    
    # Make sure the debug version exists
    echo "Looking for ${INFO_PLIST_FILE} in ${DEBUG_INFO_PLIST_FILE}"
    if [ ! -f $DEBUG_INFO_PLIST_FILE ] ; then
        echo "File GoogleService-Info.plist (debug) not found."
        exit 1
    fi
    
    # Make sure the release version exists
    echo "Looking for ${INFO_PLIST_FILE} in ${RELEASE_INFO_PLIST_FILE}"
    if [ ! -f $RELEASE_INFO_PLIST_FILE ] ; then
        echo "File GoogleService-Info.plist (release) not found."
        exit 1
    fi
    
    # Get a reference to the destination location for the plist file
    PLIST_DESTINATION=${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app
    echo "Copying ${INFO_PLIST_FILE} to final destination: ${PLIST_DESTINATION}"
    
    # Copy the appropiate file to app bundle
    if [ "${CONFIGURATION}" == "Debug" ] ; then
        echo "File ${DEBUG_INFO_PLIST_FILE} copied"
        cp "${DEBUG_INFO_PLIST_FILE}" "${PLIST_DESTINATION}"
    else
        echo "File ${RELEASE_INFO_PLIST_FILE} copied"
        cp "${RELEASE_INFO_PLIST_FILE}" "${PLIST_DESTINATION}"
    fi
  12. Select the “Debug” build scheme and build your app by pressing Cmd+B and ensure the build process completes successfully without any errors. After that, right-click on the .app file under the “Products” section of Xcode project navigator and select  “Show in Finder”. 
  13. Change the build scheme to release and build your app again. Do the same as before and inside the .app file make sure GoogleService-Info.plist file is there. 

You can also read our Blogs on Firebase:

1- Firebase Crashlytics Integration In iOS Swift (dSYM File Required Problem Resolved).

2- Firebase Authentication in iOS

Conclusion:

You now are able to use different Firebase projects for your application environments, so that data generated by each version does not get mixed with the others.

InnovationM is a globally renowned Mobile application development company in India that caters to a strong & secure Android app development, iOS app development, hybrid app development services. Our commitment & engagement towards our target gives us brighter in the world of technology and has led us to establish success stories consecutively which makes us the best iOS application development company in India.

Thanks for giving your valuable time. Keep reading and keep learning.

Leave a Reply