Refactoring is an eternal topic in software engineering. The same is true for Xcode projects, including source code and configuration files. Podfile is the common way for Xcode projects to manage dependencies. As more and more pods and targets are introduced in the project, multiple targets sharing the same pods can make Podfile easier to understand and maintain.
1 Define a Pods Collection
Create a collection, which contains all the pods you want to share among multiple targets. Include this collection in the target to introduce these pods.
def shared_pods # Comment the next line if you don't want to use dynamic frameworks use_frameworks! # Pods for PromoBee pod 'WechatOpenSDK-Swift' pod 'UMCommon' end target 'PromoBee' do shared_pods end target 'PromoBeeTests' do shared_pods end
This method is a good choice for managing common and global pods. Developers can reduce complexity and duplicated code, without worry about ommissions and inconsistency in future modifications.
2 Abstract Target
Abstract target is an alternative way for convenient target dependency inheritance.
abstract_target 'App' do # Comment the next line if you don't want to use dynamic frameworks use_frameworks! # Pods for PromoBee pod 'WechatOpenSDK-Swift' pod 'UMCommon' target 'PromoBee' target 'PromoBeeTests' end
The abstract target is not a build target in the Xcode project, but the build target contains the abstract target name to indicate the inheritance relationship.
This method should be used when multiple similar targets share the same pods conditionally. For example, a project has three targets, an iOS target, a tvOS target, and a macOS target. The iOS and tvOS target inherit from the same abstract target, which contains pods to share and work only on iOS and tvOS but not on macOS.
3 Inherit from parent target
A target can inherit dependencies from its parent target.
target 'PromoBee' do # Comment the next line if you don't want to use dynamic frameworks use_frameworks! # Pods for PromoBee pod 'WechatOpenSDK-Swift' pod 'UMCommon' target 'PromoBeeTests' end
The difference from inheriting from an abstract target is that the parent target is a build target in the Xcode project.
Usually, the unit test target inherits from the target that needs to be tested for sharing the same pods and complication configuration easily.
In summary, defining a pod collection, abstract target, and inheriting from parent target are three effective practices that multiple targets share the same pods in Podfile. For a complex project, all the methods mentioned above are even used. Open and refactor your Podfile now! Need help? Comment on this blog, and I will contact you as soon as possible.