When building mobile apps, it is very common to integrate Facebook, Google or Facebook SDKs. Managing configurations for these SDKs across different environments can become tedious and error prone. Configurations like setting the app key in info.plist (or GoogleService-Info.plist) sit outside of the scope of the code and cannot be handled programatically. As a consequence, we often end up managing the files manually and … making mistakes.

At iCarAsia, we use 4 environments: stack, staging, prepared and production. Everytime we build for one of these environments we need to change the app keys and URL scheme for Fabric, Facebook and Google. In order to limit the chances of error, we decided to automate the process.
To automate the configurations of these SDK’s following changes are required as per their documentations.

To change the facebook, google and fabric SDK’s settings for different environments following things needs to be modified each time build is required.

Facebook:

Setting the FacebookAppID in the info.plist

Setting the FacebookDisplayName in the info.plist

Facebook_info

Setting the FacebookAppID with fb prefix as the URL scheme in the info.plist

 

Facebook_URL_scheme

 

Google:

Google has its own GoogleService-Info.plist. its TRACKING_ID needs to be replaced in that file

 

Google_plist

 

Fabric:

Setting the APIkey in info.plist under Fabric key.

 

Fabric

 

 

Automation:

To automate this process we are using shell scripts which can easily be integrated in the xcode build process using the Target -> Build phases -> New run script phase

 

Adding tun script pahse

 

To invoke any script just place a call to that script file in the shell script placeholder like for example for  facebook it is the folowing code snippet in our project

# Setup Facebook Kit API Keys
. ${PROJECT_DIR}/FacebookKeyAutomationScripts/Setup_Facebook_API_Keys.sh

which in the xcode project look like this

Facebook script

 

Note: Just make sure this script phase is after the “compile sources”and “copy bundle resources” phase. So that files which needs to be modified are already copied to the target bundle.

 

Facebook script phase order

 

Now turning to the actual code (script) which modifies the files,  Since the concept of writing the script is same for every SDK so lets use the example of the facebook for illustration purpose.

 

Scripts:

To start off we always create two shell script files

Facebooj Script files

  1. Keys file : Defining the keys for different environments
  2. Automation logic file : The script to change the keys in the build depending on the build environment.

1.    Keys file

This files contains all the keys for the different envirnoments. For us its staging, stack, preprod and production.

Another benefit of using this file is that if you have different targets than you can differentiate the keys for different targets. Like we have three different targets for three countries and we use that to differentiate the keys which will be used in the second script.
${TARGET_NAME} is one of the variable available in build settings which can be used to differentiate the targets in xcode project.  For the complete list of available build settings please refer to this apple page.

 

2.   Automation’s logic file

The second phase is to use the keys and perform the required changes according to the SDK requirements. Here is the script to do that

First we define the path to the info.plist of the target at line 9 . Then import the keys files defined in step 1 to this scope so that we can use it.

Next we see if the current configuration for the project is “AppStore”. If it is indeed the AppStore build than we get he current key using the helpful utility plistbuddy which is available on mac to modify and access the plist files.

We than compare the current key with the facebook production key. If the keys matches than there is no need to change anything as the last configuration scheme for the build process was same as the current one as keys are already set properly otherwise we have to update the FacebookAppID, FacebookDisplayName and URL scheme values which is done from line 30-33.

Note:

Important thing to mention is this case URL scheme is the first element in the URL schemes array. If your URL scheme for is at second or any other position use that position like “:positionIndex:” insetead of “:0:” in the script.

After that to confirm that the values are changed correctly we get the latest values from the plist and print it to console.

You can confirm the values from the Report Navigator (cmd +8) of the xcode project.

Script Output

 

The script below line 67 is same as the explained for with different keys.

Same kind of scripts can be written for fabric and google. For you reference the links of these scripts for our app are added at the bottom.

There is one extra step for fabric  that is to upload the dsym build to the fabric which can be added as another build phase to run another script. The script for that is

Which just invokes the fabric run command as they have described in their docs. We just don’t run it for the development build as we don’t want to upload the DSYM file for crashes during the development. But for QA and production builds it get uploaded automatically.

The gists of other scripts can be found at the following links.

Facebook_keys.sh , Setup_Facebook_API_Keys.sh

GoogleAnalytics_keys.shSetup_GoogleAnalytics_API_Keys.sh

Crashlytics_keys.shSetup_Crashlytics_API_Keys.sh

Conclusion:

Automating the key change process for different environments and countries had a great effect on our workflow. We can now work with confidence, knowing the right keys will be used for each environment. It also saved us significant time on the tedious and error prone task of configuring SDKs.
We will now work on automating the build distribution process for QA and Apple. So that, for instance when QA needs a specific build for a specific country, the correct app is built and sent with one command.

 

Leave a Reply

Your email address will not be published. Required fields are marked *