Configuring Multiple Targets in an iOS Project

multiple targets

Background First, a quick rant: the company has recently taken on several outsourced projects with similar functionality, and I've been grinding through them day and night, basically turning into a contractor. The bonuses are decent, but this pace isn't sustainable. Approach Since the projects share similar functionality, the goal is to maximize code reuse. The most practical solution I've come up with is using — compiling different projects from the same codebase using multiple targets. This should be sufficient for requirements that only involve swapping out the UI theme. Steps 1. Assume you already have a project called . There will be a default target named . The basic setup and configuration for this target is assumed to be done. 2. Right-click the target and select . This creates a new target named and a corresponding plist file . 3. Rename both the target and the plist file, then use to link them, as shown. 4. Update the scheme name in . 5. Update the for . Also verify that the corresponding settings in are consistent. 6. In , search for and add . You can then use this macro in code for conditional compilation. See the code example below. 7. Modify as needed. Note: The newly added must be added under , and the unused should ideally be removed as well. 8. You can now modify and debug by selecting different targets. 9. For app icons and image assets, create a new asset catalog. Simply copy the JSON file over. It also needs to be configured in . The recommended approach is to keep shared assets in , and have each target include its own separate asset catalog. 10. For the , you can configure it like this: You may see a warning like the following. The build should still succeed, but I prefer to resolve it to be safe: Someone on proposed a solution (apparently also works): what fixed it for me was to change the configuration file setting to None for the two Pods-related targets, then run pod install again. 11. Here is an example of using the macro in code for conditional compilation based on the active target: 12. If the project uses Swift and Objective-C mixed, errors may occur — most likely because the file was not generated, or an existing one was not configured as the bridging header. Code: All code from this article can be found on my GitHub: . References: Thanks to my apprentice for helping organize the reference materials. brennanMKE/MultipleTargets