Multi-Targets usage in Xcode Projects

Satheesh Kannan
Sep 2, 2018 · 5 min read

There are many different ways to present your app’s special things to the users. One of the way to present the special feature to the user is having multi version (like Lite & Pro) of your app.

In this situation Targets in Xcode plays the major role. The other bad way to achieve this requirement is having different project for each app. But having multiple targets to a single project can generate more than one version of your app.

What is Target in Xcode

A target is an end product created by running “build” in Xcode. It might be an app, or a framework, or static library, or a unit test bundle. Whatever it is, it generally corresponds to a single item in the “built products” folder.

Creation of basic project

  • In Xcode, File -> New -> Project -> Single View Application will create a simple project after filling the required fields in the following image.

Steps to Achieve

  • Now, your project navigator looks like below.
  • You can do any basic UI and code changes in Storyboard and AppDelegate / ViewController. In this “SimpleApp”, there is a UIImageView as background Image for ViewController’s view, and a UILabel having text to differentiate the app targets as like below.
  • Now, Click ProjectName(SimpleApp) you can see the Targets section. Right click the target you want to add special features into it. Click “Duplicate”.
  • You will see the following alert. Just click “Duplicate Only”. It will duplicate the selected target.

Note: The reason this dialog exists is to provide you with an easy way to create both iPhone and iPad versions of an app within a single project, using two different targets. If this is what you wanted to do, you could select “Duplicate and Transition to iPad”. However, before you go down this route you should consider making your app universal rather than making separate iPhone and iPad versions of your apps.

  • Simply hit “Return” key, by selecting new target to rename for further uses. After renaming both targets your targets section will look like below.
  • Another one naming issue to fix is your target’s scheme. A scheme defines what happens when you press “Build”, “Test”, “Profile”, etc. Usually, each target has at least one scheme.
  • We have to rename the schemes by clicking the “Manage Schemes” from above menu. We can rename the schemes, simply by hitting “Return” key as like below. After renaming process, click close.
  • In all above process, Xcode will generate a new plist file and add to your project navigator. These plist file names are not consistent with our Target’s name. So we have to rename them, by using same “Return” key methodology. After renaming we have to setup the right plist file in Xcode Project settings. Also change the bundle display name as per the need.

Now you are good to go with multi targets..

Working on Code

Running on a single codebase, we need some programmatic way to determine which product (target) is currently running so you can make decisions like this appropriately.This is where a Preprocessor Macro comes into play.

Each Target’s Plist have some preprocessor macros to identify your working environment. We have to set a custom macro to play with this scenario.

To add a custom macro, Click ProjectName(SimpleApp) -> particular target you want add macro -> Build Settings. Then filter by word “Macro”. Then you will find Preprocessor Macros section.

You’ll see the Debug build already has a macro called DEBUG=1 here. You want to add something similar to both the Debug and Release profiles to signify this target is tied to the PRO product. Double click the values for these, then hit the ‘+’ button and enter a macro called PRO=1.

We can check this macro by code like below. In Objective — C,

#ifdef PRO

infoLabel.text = "PRO Version"

#else

infoLabel.text = "LITE Version"

#endif

Also in Swift, use “-DPRO” in “Other Swift Flags” area.

#if PRO

infoLabel?.text = "PRO Version"

#else

infoLabel?.text = "LITE Version"

#endif

Handling Images

If you are going to drag and drop any new image into the project means, the project prompt you to select the target as shown in below. In that prompt, you have to select the particular target, that you are going to use the image.

If you are using .xcassets file to handle the image means, my best advice is to use a separate .xcassets file, and a common file. Please see the below image of two .xcassets files, that having AppIcon as different.

Finally, Bingo.. You have multiple targets on your projects. Run both targets on your simulator (or device) you can export multiple apps from your single project with different features.

Please comment, if you have any doubts or you can suggest me some other ways to achieve it.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade