skip to Main Content

I have a projects with multiple schemes (Not targets).
I have Dev, QA and Prod and I want to add Firebase Analytics and Crashlytics to all of the schemes note that each scheme has its own Bundle id and different name.

How can I achieve this this?

2

Answers


  1. Chosen as BEST ANSWER

    Tested on Xcode 13.3.X

    Assuming you already have a Google Firebase account and opened an app in the Firebase console add an app in the console for iOS.

    Follow the steps and register your Bundle id for the app now for each scheme (Dev, QA and Prod) you will need to register a different app with different Bundle id and download the GoogleService-Info.plist file DO NOT rename the Plist files.

    In your Xcode project Create separate folders for each environment drag each GoogleService-Info.plist files to their folder and Uncheck Copy to target.

    In your pod file add pod 'Firebase/Crashlytics' (if you are also using analytics add the pod) and run pod install in the terminal.

    After this go to pods target (this is a bug that google suggested a workaround for) and search Apple Clang - Warnings - All Languages and set Quoted include in Framework Header to NO).

    After this Go to your target Build Settings under Build Options -> Debug Information Format set all to :

    DWARF with dSYM File

    On Build Phase tab in the Target add 2 Run Scripts.

    The first call Firebase Plist selector (or any other name you want just make sure it runs BEFORE the script to upload the dSYM) and add the following script :

    INFO_PLIST=GoogleService-Info.plist
    
    DEVELOPMENT_INFO_PLIST=${PROJECT_DIR}/${TARGET_NAME}/Environment/Dev/${INFO_PLIST}
    
    QA_INFO_PLIST=${PROJECT_DIR}/${TARGET_NAME}/Environment/QA/${INFO_PLIST}
    
    PROD_INFO_PLIST=${PROJECT_DIR}/${TARGET_NAME}/Environment/Prod/${INFO_PLIST}
    
    echo "DEV -> CHECKING in development! ${INFO_PLIST} in ${DEVELOPMENT_INFO_PLIST}"
    if [ ! -f $DEVELOPMENT_INFO_PLIST ] ; then
        echo "DEV GoogleService-Info.plist not found."
        exit 1
    fi
    
    echo "QA -> CHECKING in QA ${INFO_PLIST} in ${QA_INFO_PLIST}"
    if [ ! -f $QA_INFO_PLIST ] ; then
        echo "QA GoogleService-Info.plist not found."
        exit 1
    fi
    
    echo "PROD -> CHECKING in PROD ${INFO_PLIST} in ${PROD_INFO_PLIST}"
    if [ ! -f $PROD_INFO_PLIST ] ; then
        echo "PROD GoogleService-Info.plist not found."
        exit 1
    fi
        
    PLIST_DESTINATION=${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app
    echo "Copying ${INFO_PLIST} to final destination: ${PLIST_DESTINATION}"
        
    elif [ "${CONFIGURATION}" == "QA MyProject" ] ; then
        echo "QA -> Copied FILE : ${QA_INFO_PLIST}."
        cp "${QA_INFO_PLIST}" "${PLIST_DESTINATION}"
        
    elif [ "${CONFIGURATION}" == "Prod MyProject" ] ; then
        echo "PROD -> Copied FILE : ${PROD_INFO_PLIST}."
        cp "${PROD_INFO_PLIST}" "${PLIST_DESTINATION}"
        
    else
        echo "DEV -> Copied ${DEVELOPMENT_INFO_PLIST}."
        cp "${DEVELOPMENT_INFO_PLIST}" "${PLIST_DESTINATION}"
    fi
    

    Here you are checking for the GoogleService-Info.plist file for each scheme (note where it says /Environment/Dev QA Prod etc change it to your folder path) if the file is found then it will be added in build time and the correct Plist file will be added to the build each time.

    Now in the second script add this:

    "${PODS_ROOT}/FirebaseCrashlytics/run"
    

    And under Input Files add these 2:

    $(SRCROOT)/${DWARF_DSYM_FOLDER_PATH}/${DWARF_DSYM_FILE_NAME}/Contents/Resources/DWARF/${TARGET_NAME}
    
    $(SRCROOT)/$(BUILT_PRODUCTS_DIR)/$(INFOPLIST_PATH)
    

    Clean and build project if everything is correct when your enter the Crashlytics part in the console and simulate a crash (you can put fatalError on a IBAction or Button action to simulate) and you will be able to see your crash for each scheme you configured.

    As a note if you wish to copy a folder use :

    cp -R
    

    This will copy the folder and all its contents.

    Very important to add a / at the end of the name for example change

    INFO_PLIST=GoogleService-Info.plist
    

    to

    INFO_PLIST=MYFOLDERNAME/
    

    Kindest regards.


  2. if you have multple scheme on just one target:
    you can change plist files following the scheme like this:

    #if DEV_DEBUG || DEV_RELEASE
        let filePath = Bundle.main.path(forResource: "GoogleService-Info-Dev", ofType: "plist")
    #else
        let filePath = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist")
    #endif
        guard let fileopts = FirebaseOptions(contentsOfFile: filePath!)
        else { assert(false, "Couldn't load config file") }
        FirebaseApp.configure(options: fileopts)
    

    in this code I have two schem one is Dev other is Prod

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search