How to run multiple version of your iOS app on the same device?

Siddharth Paneri
6 min readJan 3, 2019

Product Flavor iOS

Build variants/product flavors are nothing but different versions of your app that you may release to the different user-base. But where to use it?
It may be the case that you are using different back-office URLs, separate app icons and other stuffs for all your builds and you have to maintain separate projects or maybe commenting stuff and release to all your users. What if we can achieve all of this and more in just single code/project and still be able to deliver m ultiple builds.

‘Product flavors’ is basically a terminology introduced by GOOGLE for Android apps, since Android has this functionality by default it is a bit easier to implement. But this article is about iOS and we have a lot to cover up so let’s jump to the point.

Before I start jabbering all about product flavor and how I use it, I must tell you, this post is fairly simple and can be read and referred by developers of any level but, if you have a bit of knowledge about Xcode Build settings and Schemes, trust me you are gonna kill it.

Background

Could you please make 3 copies of our app, pointing to different servers. I will need it in tomorrow’s presentation.

Project Manager

uhhh! copies?

Seriously! what are we up to?

Since you now know the background motivation of ‘product flavour’ for me, Let’s discuss what are we going to achieve.

  • Single code + 3 builds.
  • Make decisions based on app installed.
  • Distinguishing between the apps.

Basic Idea!

Starter project

Since you might not want to play around with your live projects, take this as a starter project

Bitcoin Ticker

Project Configurations

Open project, click on BitcoinTicker project, select BitcoinTicker under PROJECT go to Info > Configurations
Create new configuration Staging as a duplicate of Debug configuration, you can also rename ‘Debug’ and ‘Release ‘, Since I like to call them calling Development and Production I renamed them.

Xcode Build Settings

We will use Xcode’s User-defined settings to create some flavors that we can use inside build settings
Create a new User-Defined setting and name it BT_FLAVOR, also add value for each setting as DEV, STAGE, PROD, since we will be using the short form for all of these settings now on.

Now since we need multiple version of our app side by side, we will need different bundle identifier for each build, idea is to derive the bundle identifier from the flavor itself by suffixing the flavor type.
You can either use Fastlane to produce different bundle identifiers for our builds or you can do it manually from the Apple Developer site.

Now create a new key in User-Defined settings for bundle suffix BT_BUNDLE_ID_SUFFIX and add this value to it .$(BT_FLAVOR:lower)

See the magic!!, configurations under BT_BUNDLE_ID_SUFFIX is now named .dev, .prod, .stage

We will be appending them in product bundle identifier so we can have different bundle ids for each build of our

Product Bundle Identifier

Just edit the Product Bundle Identifier and add $(BT_BUNDLE_ID_SUFFIX) at the end of it, you will see when you add this to your bundle identifier, you will see the identifiers same as below image.

Detect active configuration in code!

We may also need to distinguish which configuration we are currently running on inside our code so we can configure our Active Compilation Conditions. Using these settings we can make decisions based on active configuration

Changing App Name and App Icons to distinguish all flavors

Click on your project in project navigator, select your Target and click on Build Settings, find Product Name, currently it’s value will be shown as $(TARGET_NAME), we need to change it to $(TARGET_NAME) $(BT_FLAVOR:lower) see below image

For changing App icons we need to do same for Asset Catalog App Icon Set Name inside B uild Settings, we edit it to AppIcon $(BT_FLAVOR:lower), this will allow us to have separate App Icons for all our flavors.

To make it work we will need to create 2 more AppIcon inside lick ‘+’ in the lower left corner and select App Icons & Launch Images andclick New iOS App Icon, this will create an AppIcon now go ahead and rename the AppIcons accordingly.

All Set, now just drop the icons sets for dev and stage inside the AppIcons asset.

How to detect configurations in code?

You will need to write below code if you want to check which configuration you are currently running.

#if FLAVOR_DEV print("Development build!") #elseif FLAVOR_STAGE print("Staging build!") #elseif FLAVOR_PROD print("Production build!") #endif

Xcode Schemes >

An Xcode scheme defines a collection of targets to build, a configuration to use when building, and a collection of tests to execute

Apple

Using scheme we will put this all in place. Just click on your Edit Scheme

Click select Run destination and build configuration

Try building the code for your desired configuration.

Oops! Compile time error?

Well since we have used CocoaPods in our project the configurations are not set for it, I would recommend you to close the project, open Terminal, perform change directory using cd and change the current directory to one where your project is stored. Once done write pod install in terminal window and wait for it to do its magic.

Go ahead and build your project, 🍺🍺🍺

One more thing I would like to add is what I do in my projects, I create multiple schemes one for each configuration and later I just select the scheme and run it!!!

Please do let me know if you liked or disliked the article and share your experiences with schemes and build configurations that you have had over the time.

Final project can be found here

Bitcoin-Ticker-Final

Originally published at http://siddharthpaneri.in on December 2, 2018.

--

--

Siddharth Paneri

iOS Applications expert with strong skills in design, architecture, UX standards, and DevOps for Mobile. Product ideology!