1

before everything, I have already read this post Use different GoogleService-Info.plist for different build schemes

But still somehow it's not working for me.

I have two Prod and dev build schemes and I have two different GoogleService-Info.plistfor them.

In my first attempt, I rename both google files to GoogleService-Info-prodand GoogleService-Info-dev. and tried to read the files directly:

#if Production
        if let filePath = Bundle.main.path(forResource: "GoogleService-Info-prod", ofType: "plist"),
           let options = FirebaseOptions(contentsOfFile: filePath) {
            FirebaseApp.configure(options: options)
        } else {
            fatalError("GoogleService-Info-Dev.plist is missing!")
        }
#elseif Development
        if let filePath = Bundle.main.path(forResource: "GoogleService-Info-dev", ofType: "plist"),
           let options = FirebaseOptions(contentsOfFile: filePath) {
            FirebaseApp.configure(options: options)
        } else {
            fatalError("GoogleService-Info.plist is missing!")
        }
#endif

        FirebaseConfiguration.shared.setLoggerLevel(.min)
        
        UNUserNotificationCenter.current().delegate = self
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { _, _ in }
        application.registerForRemoteNotifications()
        
        Messaging.messaging().delegate = self

App can run ok after that. But in logs I have this warning

[FirebaseCore][I-COR000012] Could not locate configuration file: 'GoogleService-Info.plist'.

So It can mean that filename must be GoogleService-Info.plist and it doesn't work with GoogleService-Info-prod and GoogleService-Info-dev


In my second attempt, I made all the two files to GoogleService-Info.plist, so it leads to have an error:

"Multiple commands produce GoogleService-Info.plist"

To avoid this error, I removed these two files from Build Phases > Copy Bundle Resources

and make the firebase setup function like that:

FirebaseApp.configure()
FirebaseConfiguration.shared.setLoggerLevel(.min)
        
UNUserNotificationCenter.current().delegate = self
 let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
 UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { _, _ in }
 application.registerForRemoteNotifications()
        
Messaging.messaging().delegate = self

And then add this script

GOOGLESERVICE_INFO_DEV=${PROJECT_DIR}/${TARGET_NAME}/Application/Firebase/Dev/${GOOGLESERVICE_INFO_PLIST}
GOOGLESERVICE_INFO_PROD=${PROJECT_DIR}/${TARGET_NAME}/Application/Firebase/Prod/${GOOGLESERVICE_INFO_PLIST}

# Make sure the dev version of GoogleService-Info.plist exists
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

# Make sure the prod version of GoogleService-Info.plist exists
echo "Looking for ${GOOGLESERVICE_INFO_PLIST} in ${GOOGLESERVICE_INFO_PROD}"
if [ ! -f $GOOGLESERVICE_INFO_PROD ]
then
    echo "No Production GoogleService-Info.plist found. Please ensure it's in the proper directory."
    exit 1
fi

# Get a reference to the destination location for the GoogleService-Info.plist
PLIST_DESTINATION=${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app
echo "Will copy ${GOOGLESERVICE_INFO_PLIST} to final destination: ${PLIST_DESTINATION}"

# Copy over the prod GoogleService-Info.plist for Release builds
if [ "${CONFIGURATION}" == "Release" ]
then
    echo "Using ${GOOGLESERVICE_INFO_PROD}"
    cp "${GOOGLESERVICE_INFO_PROD}" "${PLIST_DESTINATION}"
else
    echo "Using ${GOOGLESERVICE_INFO_DEV}"
    cp "${GOOGLESERVICE_INFO_DEV}" "${PLIST_DESTINATION}"
fi

BTW: I have to check the For install builds only, otherwise I will get error:

Command PhaseScriptExecution failed with a nonzero exit code

Then after run, app will crash, and it says firebase cannot find GoogleService-Info.plist- it's because I remove then from Build Phases > Copy Bundle Resources to avoid multiple file name issue.

Can anyone help how to fix this issue and add google files successfully.

BTW: I don't care about any Firebase analytics

Thank you so much

1 Answers1

1

I've implemented this in one of the apps I worked on. You're close to solving it. The way I did it was to add different GoogleService-Info.plist files in my project (I had 4 schemes and added 4 files) under separate folders. These files were not included in any target (all targets unchecked in Target Membership under Identity Inspector), they were just a part of the project.

I wrote a build phase script to copy the appropriate file based on my configuration (which in-turn is linked to my schemes) to the build. Here's an abridged version of the script that will give you an idea of how to go about it

FILENAME=GoogleService-Info.plist

DESTINATION=${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app

if [ "${CONFIGURATION}" == "MY_CONFIG_1" ]
then
    CONFIGFILE1=${PROJECT_DIR}/MYPROJ/CONFIG1DIRECTORY/${FILENAME}
    
    if [ ! -f $CONFIGFILE1 ]
    then
        echo "File not found"
        exit 1
    fi
    
    cp "${CONFIGFILE1}" "${DESTINATION}"
    
elif [ "${CONFIGURATION}" == "MY_CONFIG_2" ]
then
    CONFIGFILE2=${PROJECT_DIR}/MYPROJ/CONFIG2DIRECTORY/${FILENAME}
    
    if [ ! -f $CONFIGFILE2 ]
    then
        echo "File not found"
        exit 1
    fi
    
    cp "${CONFIGFILE2}" "${DESTINATION}"
    
fi

The key here is that I'm using the name GoogleService-Info.plist for all 4 files, and just copying the required one to the app while building.