Building for Multiple Targets in XCode

Some months ago, I needed to create new versions of my app’s. I wanted to do this without duplicating any code or projects. So I did some research and decided to figure out how to build towards multiple targets in XCode.

This is something I shied away from for a long time as I thought it would be a nightmare to figure out. What I discovered was that it’s much easier than I first thought, and also much more powerful as well.

So why do I need to do this? There are a few reasons:

  • To create different versions of your app which have different features. E.g. a ‘Lite’ or ‘Free’ version
  • To create targets which are tailored for different devices, possible with the same model and controller classes, but different view classes E.g. one target for iPhone, and another for iPad

So how do we do it?

Lets create a simple demo project.

In XCode, create a new project based on the ‘Utility Application’ template. Lets call it ‘TargetTest’.

Click on ‘Build and Run’

Notice under ‘Targets’ that there is one default target called ‘TargetTest’

We can create a new target by simply duplicating this one. Right click on that target and select ‘Duplicate’

You will notice a new info.plist copy is made for this new target. We can add target specific information here, such as changing the ‘Bundle Display Name’.

Lets rename the newly created target, TargetTest_VIP

Now, right click on the new target, and select ‘Get Info’. Under the ‘Build’ tab, ensure you have Configuration set to ‘All Configurations’.

Now we want to add a new flag under the ‘Other C Flags’ setting. We will enter -DVIPVERSION

We should also at this point, change the Product Name to a more meaningful name as shown.

Now, to see how it works, we will modify MainViewController.m slightly. In the showInfo method, we want to check which target we are running on.

So as you can see, if the C flag we defined is detected, the code will run in one direction, if not, then we go another way. We could easily give users who used in-app purchasing more features at this point for example.

Now, to test, we simply choose TargetTest_VIP from the Active Target dropdown, and ‘Build and Run’.

Now, when we click on the info button, we get the VIP message:

Now if we run again, but this time first select the Active Target, TargetTest, when we press the info button, the view flips.

You will also notice that 2 separate targets are built in your build output folder.

So overall this makes things a lot easier when it comes to distribution and allows maximum code reuse which can only be a good thing!

Download the demo project here.

If there’s anything I’m missing, please leave a comment and I can add it to the post.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • Google Plus

8 thoughts on “Building for Multiple Targets in XCode

  1. Thanks so much for explaining this, especially setting the configuration flags. That one had me stumped.

  2. HI , i can run this with simulator , but cant give the build for device ? what will be the problem?

  3. Never cross your mind linking different folders (and different implementations) per target instead of having docent of #ifdef ? I don’t know which are all the possible alternatives and I am still wondering which it is the better approach.

  4. hi,

    i donwload your code from here, but when i tested this . it’s just directly moving to FlipSide View controller . how can i see alertview that you used in your class.

Comments are closed.