Monday, June 4, 2012

How to create multiple targets in Xcode 4.2 for IOS Projects


If you want to build multiple versions of an application from a single Xcode project, you would want to cerate multiple targets. For example, when you want to release a free version and also a paid version, you should create 2 targets in that Xcode project.

This tutorial will cover 'how to add multiple targets' and manage their resources in Xcode project.

1. Create a New Target


Let's assume we have an application called 'MissionImpossible', which is a paid version. And we want to create a free version with reduced functionality or with very few playable levels.It is to note that both the paid version and free version would share the same code, with only some differences.

Firstly create a new target into the already exiting xcode project.


Let me call it as a 'MIFree'


Now what do you see is it has two different entry points i.e AppDelegate files

Depending upon the build target you have selected it will create the installer under products section.

Inside 'Build Phases' you can choose corresponding source files to compile, corresponding libs and corresponding bundle sources like ' .plist' files.


2. Info.plist


Now as the two resource files are separate from each other we can add necessary icon file, product name for each target.



3. Writing Preprocessor code

Preprocessor codes are used to determine which code would be used during compile time. You may want different targets to run different section of the codes using preprocessor codes.

Now 2 very important things here

1. Add User Defined Macro
By clicking 'Add Build Setting' and inside that click on 'Add User-Defined Setting'. It will one a new setting section in editable mode like shown in following image.


Type your own defined macro name without any space and it's value. 


2. Do not forget to initialize it, like shown in following figure.

With the flag LITE_VERSION defined, we could now write codes that will be compiled for the different targets. An example is shown in following fig.



When the active target is selected and being run, it will work and print correctly.



Now On a very important node, If suppose you already have an xcode project which suppose you have been monitoring since last few weeks/months then instead of step no.1 from this blog use following step.


Duplicate Target


- Right click on the existing target and say 'Duplicate'. Remember all the setting that has been implemented on this target will reflect as it is on this newly appeared target, that includes same product name, icon file and info.plist as well.

- Change them individually picking 1 by 1.

- Rest steps would remain similar i.e. Writing Preprocessor Code
This step is very much important and one may realize even though keenly following all the instructions still most of the time it does not work as expected.
Remedy is simple for me it work when first i select the necessary 'build target' , clean the project and quit the xcode project. Once reopened you will tend to observe expected results.(At least for me it worked like Gem)


Regards,


No comments: