Targets are an extremely yet totally undocumented feature of Xcode. They allow you to write code once, and then build multiple “versions” of the same code base. This makes maintenance and code updates extremely easy across more than “product”.
I use those quotation marks because usually an “Xcode Project” equals a “Product” (such as one iPhone app, or one Mac app). But really it’s the Target that defines the product, and it is feasible to write code once and use it to create several “product versions” of your app, for example:
- a Lite and a Pro version
- an iPad and an iPhone version
- an iPad/iPhone version and a Mac version
- different apps altogether based on the same code family
Targets are extremely easy to use, let’s see how in Xcode 4.6:
Adding a Target
When you create a new Xcode Project, it already comes with one Target by default. You can add another one by clicking on the big blue Project Bar in the File Inspector on the left. At the bottom you’ll see a large plus sign that reads “Add a Target”. Do that and Xcode will bring up a dialogue that looks like the one when you create a new Project.
Select what you’d like to add, give it a name an identifier and hit Finish. Now you’ll have a new Target.
You’ll also have a new Group which includes all the files that came with the template you chose. So right now it’s like having two completely different apps inside one Xcode Project. That’s interesting, but you’ll probably want to share resources to make it useful.
Sharing New Resources
Try adding a new file to your Project to bring up the following dialogue:
I’m adding a simple image file here. Everything looks as usual, but notice that now you can select which Target this new resource belongs to. See how you can check multiple targets that can share a new resource – neat, huh? This is great for new resources, but what happens to the ones you already added to your project?
Sharing Existing Resources
If you already have resources in your Project (such as image files and Storyboards) and would like to make them available to your new target, click on each file in question, head over to the File Inspector in the right hand pane (it’s the left most one that looks like a piece of paper), and scroll down to Target Membership. Tick as appropriate:
This works well with most files, but code files (such as App Delegate and View Controllers) can’t be changed this way. In all likelihood you’ll want to share those too. You can’t add them to your new target either, as they’re greyed out if you try. What’s worse, you’ve probably spotted that after adding a new target you may end up with a second set of files – so what is a boy to do?
A tad hacky if I may say so myself, let’s go through this step by step:
- On your new Target, go and delete the files you don’t want (say your App Delegate and View Controller)
- Make sure you trash those files
- Now head over to your first target, and remove those files as well
- DO NOT trash them, just remove the references
- Now add those files again, this time ticking both targets
And that’s it! Now you can share files that Xcode didn’t want you to.
Building Each Target
Each Target behaves like a separate app, and you can choose which one you want to run by clicking your Project Name next to the device you’d like to run it on. In my case, it’s called TargetTest (confusingly). This will bring up a drop down of each Target you can run:
That’s all I know about Targets – have fun!