Adding multiple targets to an Xcode project
You don’t have to limit your Xcode project to just one app. Thanks to Apple, you can build multiple apps based on one project. This can be done via targets. In this tutorial, I’ll show you how to add many targets to an Xcode project. Not only new apps but also extensions, widgets, etc. Let’s see, how it works.
Table of content
- What are Xcode targets?
- How to create app target
- Cloning app targets - possible problems
- Multiple targets in Xcode projects: Endnote
What are Xcode targets?
As you probably know, Xcode allows for adding multiple targets to a project.
You can use them for targeting your Watch app, as well as for app extensions or widgets. Targets can be also used for managing schemes. For example, they can be used for adding a release target and to start targeting your app. Also, they enable you to duplicate your app. This can be very useful if you need to create two or more apps that share the same business logic or views.
For checking how many options of targets are available, go to Project Navigator and click plus button in targets level.
How to create app target?
If you have already created an app in your Xcode project, adding a new target is very easy.
By selecting your project name in the Project Navigator, you open the project editor.
Here you have two levels:
- project level
- and targets level.
Targets should contain your current app.
Now, you have two ways to create a new target.
You can simply click the + button and create a new target, or right-click with a mouse on the current target and duplicate it.
The second option is very useful if you want to add an app which will share code with the previous one. Then, by selecting or deselecting Target Membership, you can decide which files should be available in the just-created app.
You can access Target Membership by selecting file and opening the right menu in Xcode (the menu is called Inspectors). Then, in File Inspector at the bottom of the menu, find Target Membership.
It’s very important to check all files in your project because without this you won’t be able to build a new app target. Or, in other cases, the app will crash due to the lack of access to specific files in your project.
Duplicating your current app needs a closer look because surely there will be more dependencies and problems to be resolved.
Examples? In one of my projects, I had an iOS app and a tvOS app that shared business logic and views. As you can predict, there are many differences between iOS and tvOS designs. tvOS doesn’t use many classes or class types that are used in iOS - and vice versa.
Cloning app targets - possible problems
Many problems may occur during cloning targets.
I’ll focus on the most frequent ones.
Firstly, you need to know that new Info.plist files are created during cloning an app target. Xcode puts them in the main folder, while usually, they’re in the project folder. So you can move them to a specific folder and rename them.
Then, after cloning the target, you should check Build Settings in your new target. In Search type Info.plist. Then you should change the file path to Info.plist file. Of course, you should also check the Info.plist file and see if it contains information about the previous target - and change it.
When I duplicate targets, I always check Build Settings by typing the name of a previous target. This lets me know how many things I should change or fix.
Xcode usually doesn’t clone Entitlements, so you should do it yourself for the newly created target. Then you should view the Entitlements file to check, if there are things to modify, e.g. the app groups identifier.
If you duplicate the existing app target, you’ll probably want to change its app icon. Or the app launching screen. You can add it in the Assets or Storyboards folder, and then change it in General target settings.
If your app has its targets - like Watch target or Widget target - you should check Build Phases in your project. In Dependencies you’ll see other targets attached. After duplicating the app target, your new target won’t point to new Watch or Widget targets. Instead of this, it will point to previous targets. You should change it, if you want your app to work properly. You should also check other tabs in Build Phases that may copy stuff from the previous target. Also, check GoogleServices-Info, if you use Analytics or other features from Google.
If your app uses CocoaPods or other dependency managers, take a look at these. You should add your app target if you want your new app to use functionalities from the dependency manager. Otherwise, you need to check places in your code, in which they’re used, and split files for targets.
E.g., if you don’t want to use Google Analytics in your new app target, you should add an extension for AppDelegate and remove it. Otherwise, your app won’t build or will crash during its use.
Multiple targets in Xcode projects: Endnote
In my final thoughts, I’d like to point out that adding new targets can be a stressful experience.
Especially when your current project is quite big. You should carefully check all files and dependencies because even if you properly build a new app target, problems during the application’s usage may occur.
In my opinion, most important thing is to check Build Settings and Build Phases, and then the app logic.
I hope this tutorial will help you!