Wednesday, April 6, 2016

Handling Multiple Targets & Environments in iOS


Hey Folks,

Finally writing something that I wanted to do from long time.
Handling multiple targets in an iOS Project that deal with multiple environments.

Lets Start.

1) Create a New Project or you can start with an existing Project.
    I will be using new one named HelloGradle which I will be using in Gradle integration also.

2) So in my Project i was provided with a default target as HelloGradle. I renamed it as "A".

3) Now lets add more Targets.

  • Click on Project.
  • Right Click on any Target.
  • Duplicate.
  • Rename it as "B".
  • Perform this task to add more Targets. I added 6 in total "A - F".




4) Name still not changed : You will observe that Target names are still not changed at all places
Go to Manage Schemes. It will show all targets. Select a Target press Enter change Name and Press Enter.




5) Now rename the plist files associated with each Target.
  • Click on Target.
  • Build settings.
  • Packaging.
  • Rename plist file for all targets as A-Info.plist , B-Info.plist .......... F-Info.plist.


                                                         



6) Build and Run : It will work fine as usual. Currently there are only 2 build Configurations namely "Debug" & "Release"

7) Now I wish to add Multiple Build Configurations or you can say Environments. 
    Remember : Build configurations are project wise not Target wise.
  • Project
  • Go to Info Tab
  • Click Editor
  • Add Configuration
  • Duplicate Debug Configuration

OR
  • Click + below Configurations
  • Duplicate Debug Configuration




8) Now we have multiple Targets & multiple Build Configurations 




9) Now the question arises how to store environment specific values. For this do this..
  • Create plist for all Environment & for all Targets. 
  • Ensure that name of plist file remains same.
  • Have a look @ the structure.
  • Remember to follow the naming conventions.
  • Inside plist file you can add values that are environment specific.
  • Remember to add one value in all plists ENVIRONMENT : TARGET_Dev / TARGET_QA / TARGET_Prod depending on Target & environment folder.


10)  Now to establish relation between plist file and build configuration
  • Target
  • Build Settings
  • Click on + next to  ALL | Combined | Levels + 

  • Add User defined Settings
  • Name it as Build_Env
  • Provide values as shown in below image



11) Choose any Build Configuration from Edit Scheme
12) Build & Run your project
13) And you are Done
14) Now from AppDelegate read your environment.plist file and set values accordingly.



Hope you did not find it BORING :P







2 comments:

  1. Thank you for giving this type of nice article.I am very glad to inform you that it is very understand everything that you said.Really i wish to thank you.It is nice post with understanding a lot.Writing services are the better service that provides detailed and effective information related to educational basis.

    ReplyDelete
    Replies
    1. Thanks so much. Extremely sorry for delayed response

      Delete