How To Create Build Variant in Xcode

Posted By : Aditya Kumar Sharma | 18-Apr-2018

Build variant is used when we want to build our app with various settings or Same app design but different App names and Server urls. Like we can make different builds for the development and staging. So this can be done by creating different targets. In Android, this can be achieved by setting different product flavors.

 

Creating different targets is a little bit confusing, so we will discuss another solution to define Xcode configurations in our project settings. So let’s follow these steps:

 

1.   Open the Project and look for Configurations section:


Now Click + and add another Debug and Release configurations

 

2. Now  we will be setting up the bundle id for different configuration:

  • So goto Targets > Build Setting > Project Bundle Identifier
  • Set bundle id for different Configurations.
 
 

3. Changing the App Name:

 

So for changing the App Name for the different configuration, we need to again open Targets > Build Setting > Product Name.

 

 

4. Create user-defined values:

 

  • We need to create the user-defined values to get different configuration values at runtime. So here I am doing to get different App Name at runtime. So go to:
  • Targets > Build Setting > User- Defined
  • Click + on the top of the view and select Add User-Defined Setting and give a name to it. Like :  >  WHITELABEL_TYPE
  • Change the values of the new added Configurations.  

 

 

5. Create Swift flags:

 

In order for Swift able to read the User-Defined, we need to create Swift-flag: 

6. Create Scheme:

  •  
  •  
  • Scheme is the way to run the configuration we want by a single click.
  • So just go to Project > New Scheme > give a new name to the scheme.

 

 

7. Manage Scheme:

 

Select the new scheme and click Manage Scheme, scroll down to new Configuration and select the shared in front of it and close the pop-up. Remember to check Shared checkbox.

 

 

8. Edit Scheme:

 

Change the configurations of all Build, Run, Test, Profile, Analyze, Archive and set the required configuration.

 

 

9. Change the APP icon and Launch Image:

 

As we have created different Project with same code so we need to change the app icon and Launch Image too.

 

10. Access the configuration in code:

 

Now if we want to access the configuration code and make changes accordingly then we can follow this:

 #if FirstOne

    	 //Will run this for only FirstOne

    #elseif SecondOne

         // Will run this for only SecondOne
    
    #else

   	 // Will run this for else cases

    #endif
 

Thanks 

 

 

 

 

 

About Author

Author Image
Aditya Kumar Sharma

Aditya is a bright iOS developer, have knowledge of objective C, swift, swift 3, JSON, Core data and iPhone development. Apart from that he loves to travel and explore new things.

Request for Proposal

Name is required

Comment is required

Sending message..