Hey I am playing with the new firebase iOS SDK, in my project, I have only one target, I created two configurations, Debug and Release, with different bundler identifier, but seems like the config file downloaded from firebase only support one bundle identifier.
So anyone know how to use firebase within a multiple bundle identifier xcode project?
Thanks!
Update: see Firebase official documentation https://firebase.google.com/docs/projects/multiprojects
Updated with an easier solution:
My first attempt that didn't solve the issue:
I renamed the second GoogleService-Info.json to something else and used following code to configure Firebase from AppDelegate.
// Swift code
#if STAGING
let firebasePlistFileName = "GoogleService-Staging-Info"
#else
let firebasePlistFileName = "GoogleService-Info"
#endif
let firbaseOptions = FIROptions(contentsOfFile: NSBundle.mainBundle().pathForResource(firebasePlistFileName, ofType: "plist"))
FIRApp.configureWithOptions(firbaseOptions)
If I run the Staging target, I will receive Firebase's complaint about 'Could not locate configuration file: 'GoogleService-Info.plist'.'. But then it says '<FIRAnalytics/INFO> Firebase Analytics v.3300000 started'.
As I checked on the Firebase dashboard, both production and staging apps have incoming user events being logged.
The above-mentioned configureWithOptions method is not documented in Firebase documentation, I figured it out by checking its source code. I'm not sure about what could be the downside of calling this. I would love to hear other approaches.
I've implemented something similar, as I've had two schemes for a single target that have different bundle identifiers. In my examples below, I have two different schemes, one for UAT and one for PROD.
Create the two GoogleService-Info.json files, and put them into your project directory (not Xcode project) in different folders, e.g.
ROOT/config/UAT/GoogleService-Info.json
ROOT/config/PROD/GoogleService-Info.json
Then add the files to your Xcode project like so:
Now you need to add a Run Script in your Build Phases. This will need to be added before the Compile Sources stage:
This Run Script takes the appropriately located json file and duplicates it into the build app directory, meaning Firebase/Google will identify it identically to how it would identify the file in a single identifier setup.
isUAT=`expr "$GCC_PREPROCESSOR_DEFINITIONS" : ".*UAT=\([0-9]*\)"`
RESOURCE_PATH=${SRCROOT}/${PRODUCT_NAME}/config/PROD
if [ $isUAT = 1 ]; then
RESOURCE_PATH=${SRCROOT}/${PRODUCT_NAME}/config/UAT
fi
BUILD_APP_DIR=${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app
echo "Copying all files under ${RESOURCE_PATH} to ${BUILD_APP_DIR}"
cp -v "${RESOURCE_PATH}/"* "${BUILD_APP_DIR}/"
I would estimate you can use this same logic for Google Analytics as well, which uses a similar json config file setup.
Better approach if you have single target is to just name your configs appropriately and load them as below. Seems to work fine for UAT environment example : Swift 3.0
var fireBaseConfigFile = Bundle.main.path(forResource: "GoogleService-Info-PROD", ofType: "plist")
#if UAT
fireBaseConfigFile = Bundle.main.path(forResource: "GoogleService-Info-UAT", ofType: "plist")
#endif
guard let firOptions = FIROptions(contentsOfFile: fireBaseConfigFile) else {
assert(false, "Failed to load Firebase config file")
return
}
FIRApp.configure(with: firOptions)
Add both Google Service plists to your project, with different names:
Then for each Build Scheme, select Edit Scheme and add a unique value for an environment variable like so:
So the other build scheme's BUILD_FOR environment variable would be set to "DEV", for example.
Then, you would check for this variable in App Delegate and configure the FIRApp according to which scheme was built:
let buildFor = ProcessInfo.processInfo.environment["BUILD_FOR"]! as String
var firebasePlistFileName = "GoogleService-Info"
if buildFor == "PROD" {
firebasePlistFileName = "GoogleService-Prod-Info"
}
let firbaseOptions = FIROptions(contentsOfFile: Bundle.main.path(forResource: firebasePlistFileName, ofType: "plist"))
FIRApp.configure(with: firbaseOptions!)
I did store 2 GoogleService-Info with different names:
GoogleService-Info.plist
for productionGoogleService-Info-Debug.plist
for development And then go to Build Phases
, add new run script:
if [ "${CONFIGURATION}" == "Release" ]; then
cp -r "${PROJECT_DIR}/PathToYourGoogleServiceInfoFile/GoogleService-Info.plist" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/GoogleService-Info.plist"
echo "Production plist copied"
elif [ "${CONFIGURATION}" == "Debug" ]; then
cp -r "${PROJECT_DIR}/PathToYourGoogleServiceInfoFile/GoogleService-Info-Debug.plist" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.app/GoogleService-Info.plist"
echo "Development plist copied"
fi
Using Objective-C, in your AppDeletage.m:
NSString *filePath;
#ifdef DEBUG
filePath = [[NSBundle mainBundle] pathForResource:@"GoogleService-Info-Debug" ofType:@"plist"];
#else
filePath = [[NSBundle mainBundle] pathForResource:@"GoogleService-Info-Production" ofType:@"plist"];
#endif
FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:filePath];
[FIRApp configureWithOptions:options];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With