Tuesday, January 24, 2012

Creating multiple targets in an iOS project

There can be many reasons why you would want to create multiple targets for an iOS project.

- You may want a separate Release Target and a Debug target so you can restrict logging to the Release target
- You may want to build a separate target for testing the application
- You may want to build a slightly different version of the current application (eg: Free and Paid versions for the same app)

The advantage of maintaining one project with multiple targets for such cases is quite obvious, when there are code changes that should apply to both applications you don't need to repeat those changes changes in separate projects.

Given below are the steps you need to follow to make a new target for an iOS project in XCode (I am using Xcode 3.2 but the steps should be similar for Xcode4).

1. Create a new target

From xcode menu select Project -> New Target (the dialog below will open).

Select 'Cocoa Touch - Application' and then NextSet target name (eg: 'NewTarget')
Check that the project is defined correctly and then click Finish to add the new target.
Now the new target will be visible in the targets sections of Groups&Files pane (project file view).

2. Copy files from existing target

Since you are making a new target for the same application code you need to copy over the files in the existing target of the project. Select and drag over the files in the following folders to the 'NewTarget'.

Copy Bundle Resources
Compile Sources
Link Binary With Libraries

After copying make sure the folders Copy Bundle Resources, Compile Sources, Link Binary With Libraries in the 'NewTarget' don't have any duplicate files. Checking this will help you avoid compiler warnings.

3. Set build settings

Before you can build and run the new target you created, you need to set the following build settings correctly. To change build settings double click the 'NewTarget' icon (this will open the target's properties dialog - see image below) and select the Build tab.

- Check that Base SDK is set correctly
- 'Compile Sources As' should be set to Objective-C (if it is set to 'According to file type' this will likely cause compile errors)
- Check the 'Info.plist File' is set correctly. You can edit this file to change the 'NewTarget' settings such as icon, launch screen etc.

4. Run the NewTarget!

Now from the xcode menu select Project -> Set Active Target and select the 'NewTarget'.
Build and run:)

Creating new targets in this way will create a new .app file (new executable) which you can use for application testing or release.